CakePHP 3 Cheat sheet


Para quem está vindo do CakePHP 2, segue algumas mudanças.

How To

Controller :
2.x => app/controller/nameController.php
3.x => src/controller/nameController.php

Model :
2.x => app/model/mymodel.php
3.x => src/model/table/mymodelTable.php

View
2.x => app/view/NameController/myview.ctp
3.x => src/Template/nameController/myview.ctp



Setup Class Controller:

namespace App\Controller;
use Cake\Controller\Controller;
class myController extends Controller
{
      public function index()
      {
      }
}

Setup Class Model:src/Model/Table/myTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
class myTable extends Table
{
}

use Cake\ORM\TableRegistry;
// Now $articles is an instance of our ArticlesTable class.

$articles = TableRegistry::get('my');

namespace App\Model\Entity;
use Cake\ORM\Entity;
class Article extends Entity
{
}

use Cake\ORM\TableRegistry;
// Now an instance of ArticlesTable.
$articles = TableRegistry::get('Articles');
$query = $articles->find();
foreach ($query as $row) {
    // Each row is now an instance of our Article class.
    echo $row->title;

}


CRUD (Create Read Update Delete)

Add Record :
public function add()
{
     $myEntity = $this->User->newEntity();
     $myuser = $this->User->patchEntity($myEntity, $this->request->data);
     $this->User->save($myuser);
     $this->redirect(array('controller'=>'main','action'=>'index'));
}

Edit Record :
public function edit($id = null)
{
    $myuser = $this->User->get($id);
    if ($this->request->is(['post', 'put'])) {
        $this->User->patchEntity($myuser, $this->request->data);
        if ($this->User->save($myuser)) {
            $this->Flash->success(__('Your myuser has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your user table.'));
}

Delete Record :
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$myuser = $this->User->get($id);
$this->User->delete($myuser);
$this->redirect(array('controller'=>'main','action'=>'index'));
}


Read Record :
// Manual Join //
$myuser = $this->User->find('all', array('field' => array('User.*,Userdetail.WORKS'),
     'joins' => array(array('table' => 'Userdetail ',
                                                             'alias' => 'Userdetail',
                                                                          'type' => 'left',
                                                                          'foreignKey' => true,
                                                                          'conditions'=> array('User.ID = Userdetail.ID'))),
                                                                          'limit' => 10,
      ));

$myuser->hydrate(true);
$this->set('myuser',$myuser);

// Non - Join with Where condition
$myuser = $this->User->find('all')->where(['User.ID'=>'1']);


Display In View :

   

ID ?>


USERNAME; ?>


PASSWORD; ?>



Form->postLink(
                'Delete',
                ['action' => 'hapus', $myuser->ID],
                ['confirm' => 'Are you sure?'])
            ?>
Html->link('Edit', ['action' => 'edit', $myuser->ID]) ?>



   
أحدث أقدم