CakePHP Model謫堺ス

bake

繧ス繝シ繧ケ逕滓

DB險ュ螳壹ッ縲CakePHP 繧、繝ウ繧ケ繝医シ繝ォ繝サ險ュ螳/DB縲榊盾辣ァ

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

竊達ake All complete
縺ィ陦ィ遉コ縺輔l繧九→OK

逕滓千オ先棡荳隕ァ


笏敗rc
縲笏尿pplication.php
縲笏任onsole
縲縲笏祢nstaller.php
縲笏任ontroller
縲縲笏尿ppController.php
縲縲笏忍rrorController.php
縲縲笏捻agesController.php
縲縲笏TestsController.php
縲縲笏任omponent
縲縲縲笏覇mpty
縲笏熱odel
縲縲笏韮ehavior
縲縲縲笏覇mpty
縲縲笏忍ntity
縲縲縲笏Test.php
縲縲笏乃able
縲縲縲笏TestsTable.php
縲笏粘hell
縲縲笏任onsoleShell.php
縲笏乃emplate
縲縲笏忍lement
縲縲縲笏認lash
縲縲縲縲笏播efault.ctp
縲縲縲縲笏覇rror.ctp
縲縲縲縲笏敗uccess.ctp
縲縲笏忍mail
縲縲縲笏派tml
縲縲縲縲笏播efault.ctp
縲縲縲笏杯ext
縲縲縲縲笏播efault.ctp
縲縲笏忍rror
縲縲縲笏覇rror400.ctp
縲縲縲笏覇rror500.ctp
縲縲笏猫ayout
縲縲縲笏蚤jax.ctp
縲縲縲笏播efault.ctp
縲縲縲笏覇rror.ctp
縲縲縲笏排ss
縲縲縲縲笏播efault.ctp
縲縲縲笏忍mail
縲縲縲縲笏派tml
縲縲縲縲縲笏播efault.ctp
縲縲縲縲笏杯ext
縲縲縲縲縲笏播efault.ctp
縲縲笏捻ages
縲縲縲笏派ome.ctp
縲縲笏Tests
縲縲縲笏add.ctp
縲縲縲笏edit.ctp
縲縲縲笏index.ctp
縲縲縲笏view.ctp
縲縲笏之iew
縲縲縲笏尿jaxView.php
縲縲縲笏尿ppView.php
縲縲縲笏禰elper
縲縲縲縲笏覇mpty

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')
縲縲縲縲縲縲->requirePresence('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')

縲縲縲譁蟄怜
縲縲縲->scalar('col1')

縲縲縲遨コ蛟、繧定ィア蜿ッ
縲縲縲->allowEmpty('col1', 'create');

縲縲縲遨コ蛟、繧呈悴險ア蜿ッ
縲縲縲->notEmpty('col1');

縲縲縲蛻励′繝繝シ繝悶Ν縺ォ蟄伜惠縺吶k莠
縲縲縲->requirePresence('col1', 'create')

縲縲縲豈碑シ
縲縲縲->add('col1', 'comparison', ['rule' => ['comparison', '>', 10]])

縲縲縲譛螟ァ譁蟄玲焚
縲縲縲->maxLength('col1', 100)
縲縲縲->add('col1', 'length', ['rule' => ['minLength', 10]])

縲縲縲譛蟆乗枚蟄玲焚
縲縲縲->minLength('col1', 100)
縲縲縲->lengthBetween('col', [4, 8])
縲}
}

Follow me!

谺。縺ョ險倅コ

CakePHP Element謫堺ス