CakePHP Model操作

bake

ソース生成

DB設定は「CakePHP インストール・設定/DB」参照

cd C:\xampp\htdocs\cakephptest
bin\cake bake all tests

→Bake All complete
と表示されるとOK

生成結果一覧


└src
 └Application.php
 └Console
  └Installer.php
 └Controller
  └AppController.php
  └ErrorController.php
  └PagesController.php
  └TestsController.php
  └Component
   └empty
 └Model
  └Behavior
   └empty
  └Entity
   └Test.php
  └Table
   └TestsTable.php
 └Shell
  └ConsoleShell.php
 └Template
  └Element
   └Flash
    └default.ctp
    └error.ctp
    └success.ctp
  └Email
   └html
    └default.ctp
   └text
    └default.ctp
  └Error
   └error400.ctp
   └error500.ctp
  └Layout
   └ajax.ctp
   └default.ctp
   └error.ctp
   └rss
    └default.ctp
   └Email
    └html
     └default.ctp
    └text
     └default.ctp
  └Pages
   └home.ctp
  └Tests
   └add.ctp
   └edit.ctp
   └index.ctp
   └view.ctp
  └View
   └AjaxView.php
   └AppView.php
   └Helper
    └empty

TestsTable.php

<?php
namespace App\Model\Table;

use Cake\Validation\Validator;

class TestsTable extends Table
{
  public function initialize(array $config)
  {
    parent::initialize($config);

    $this->setTable('tests');
    $this->setDisplayField('Col1');
    $this->setPrimaryKey('Col1');
  }
  public function validationDefault(Validator $validator)
  {
    $validator
      ->integer('Col1')
      ->allowEmpty('Col1', 'create');
    ~
    $validator
      ->dateTime('Col4')
      ->('Col4', 'create')
      ->notEmpty('Col4');
    return $validator;
  }
}

Test.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Test extends Entity
{
  protected $_accessible = [
    'Col2' => true,
    'Col3' => true,
    'Col4' => true
  ];
}

TestsController.php

<?php
namespace App\Controller;

use App\Controller\AppController;

class TestsController extends AppController
{
  public function index()
  {
    ~
  }

  public function view($id = null)
  {
    ~
  }

  public function add()
  {
    ~
  }

  public function edit($id = null)
  {
    $test = $this->Tests->get($id, [
      'contain' => []
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
      $test = $this->Tests->patchEntity($test, $this->request->getData());
      if ($this->Tests->save($test)) {
        $this->Flash->success(__('The test has been saved.'));

        return $this->redirect(['action' => 'index']);
      }
      $this->Flash->error(__('The test could not be saved. Please, try again.'));
    }
    $this->set(compact('test'));
  }

  public function delete($id = null)
  {
    ~
  }
}

edit.ctp

<nav class="large-3 medium-4 columns" id="actions-sidebar">
  <ul class="side-nav">
    <li class="heading"><?= __('Actions') ?></li>
    <li><?= $this->Form->postLink(
        __('Delete'),
        ['action' => 'delete', $test->Col1],
        ['confirm' => __('Are you sure you want to delete # {0}?', $test->Col1)]
      )
    ?></li>
    <li><?= $this->Html->link(__('List Tests'), ['action' => 'index']) ?></li>
  </ul>
</nav>
<div class="tests form large-9 medium-8 columns content">
  <?= $this->Form->create($test) ?>
  <fieldset>
    <legend><?= __('Edit Test') ?></legend>
    <?php
      echo $this->Form->control('Col2');
      echo $this->Form->control('Col3');
      echo $this->Form->control('Col4');
    ?>
  </fieldset>
  <?= $this->Form->button(__('Submit')) ?>
  <?= $this->Form->end() ?>
</div>

表示

http://localhost:8080/cakephptest/tests/

CRUD

プログラム言語 データベース操作」参照

別モデル参照

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;

class HelloController extends Controller
{
 public function initialize()
 {
  $this->Tests = TableRegistry::get('tests');
  $data = $this->paginate($this->Tests);
 }
}

バリデーション

実装方法

class TestsTable extends Table
{
 public function validationDefault(Validator $validator)
 {
  $validator
   数値
   ->integer('col1')
 }
}

バリデーション一覧
機能 コード
数値である事 integer(‘col1’)
文字列である事 scalar(‘col1’)
空値を許可 allowEmptyString(‘col1’, null(又はエラー時メッセージ), ‘create’);
空値を未許可 notEmptyString(‘col1’);
列がテーブルに存在する事 (‘col1’, ‘create’)
比較 add(‘col1’, ‘comparison’, [
 ’rule’ => [‘comparison’, ‘>’, 10]
])
最大文字数 maxLength(‘col1’, 100)
add(‘col1’, ‘length’, [
 ’rule’ => [‘minLength’, 10]
])
最小文字数 minLength(‘col1’, 100)
lengthBetween(‘col’, [4, 8])
CakePHP

次の記事

CakePHP Element操作