[Cakephp] Cheatsheet Cakephp 3


Description
      this is cheatsheet to fast learn migration from cakephp 2.x to 3.x.


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 :
<?php foreach ($myuser as $myuser): ?>
    <tr>
<td><?php echo $myuser->ID ?></td>
<td><?php echo $myuser->USERNAME; ?></td>
<td><?php echo $myuser->PASSWORD; ?></td>
<td><?php //debug($myuser) ?></td>
<td>
<?php echo $this->Form->postLink(
                'Delete',
                ['action' => 'hapus', $myuser->ID],
                ['confirm' => 'Are you sure?'])
            ?>
<?php echo $this->Html->link('Edit', ['action' => 'edit', $myuser->ID]) ?>
</td>

    </tr>


Notes : the most think you must know cakephp 3.x : Installation , Location Files (path controller , model , view) and CRUD. and other feature same as ver 2.x. you just need some adaptation.