cakephp

Cakephp 3 - Programmer Sheet

Installation
composer require --dev cakephp/bake:~1.0

PhpStorm comes with composer built in.
To install CakePHP via PhpStorm, use the File > New > Composer Project. Then filter for CakePHP and select the cakephp\app composer project.

CakePHP Console
bin/cake (use bin\cake for Windows) Run the CakePHP console from project root
bake, migrations, i18n, orm_cache, plugin, routes, server, behavior_time, console, orm Possible "shells"
bin/cake server Run Cake Server (usually on localhost port 8765)
bin/cake migrations -h Run migrations shell (with help)
bin/cake bake.bake -h Run bake with plugin prefix
Bake Shell Common Options
--help or -h help
--verbose or -v Verbose output
--quiet or -q Quiet output
--plugin or -p Plugin to bake into
--force or -f Force overwrite of existing files
--connection or -c Datasource connection to get data from (default: default)
--theme or -t Theme to use when baking
--no-test Do not bake a test skeleton
--no-actions Do not bake generic CRUD action methods
Server Shell
bin/cake server Run server shell with (defaults)
bin/cake server --help Run server shell and get help
bin/cake server --port 9999 Run server shell on port 9999
bin/cake server --host 10.0.0.2 Run server shell using IP of 10.0.0.2
bin/cake server --document_root dummy/app/root Run server shell with document root dummy/app/root
bin/cake server --verbose Run server shell with verbose output
bin/cake -H 10.0.0.2 -p 9999 -d dummy/app/root -v example
Bake Shell: Model
bin/cake bake model Run bake model shell and list all tables
bin/cake bake model Friends Bake model class for Friends table
bin/cake bake model --help Run bake model shell and get help
bin/cake bake model [subcommand] [options] [<ModelName>] Generalised use:
other than the default ones Options
--table <TableName> Table name if "non-conventional" table naming used
--no-entity Disable generation of Entity class
--no-table Disable generation of Table class
--no-validation Disable generation of validation rules
--no-rules Disable generation of a rules checker
--no-associations Disable generatin of associations
--no-fields Disable generation of fields
--fields <fieldname>[,<fieldname>...] Comma separated list of fields to make accessible
--hidden <fieldname>[,<fieldname>...] Comma separated list of hidden fields
--primary-key <key-field>[,<key-field>...] Set the primary key field or fields (comma separated)
--display-field <field-name> Display field if you would like to select one
Cake Routes Shell
bin/cake routes List of all Routes
bin/cake routes check route-and-options URL Parsing Testing
bin/bake routes check /bookmarks/edit/1 example
bin/cake routes generate controller:controllername action:action other options URL Generation Testing
bin/cake routes generate controller:Bookmarks action:edit 1 example
bin/cake routes check "/bookmarks/?page=1&sort=title&direction=desc" example with string parameters
Bake Shell: Controller
cake bake controller [subcommand] [options] [<name>] Bake a controller skeleton
bin/cake bake controller MyController Bake the My controller with CRUD actions
cake bake controller all Bake all controllers with CRUD methods
Options
--plugin or -p Plugin to bake into
--force or -f Force overwrite of existing files
--connection or -c Datasource connection to get data from (default: default)
--theme or -t Theme to use when baking
--components Components to use (comma separated)
--helpers Helpers to use (comma separated)
--prefix Namespace/routing prefix to use
--no-test Do not bake a test skeleton
--no-actions Do not bake generic CRUD action methods
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>

We use cookies!🍪

We value your privacy and use cookies to enhance your browsing experience, personalize content, and analyze site traffic. To learn more about how we use cookies and manage your preferences, review our Cookie Policy. By clicking 'Accept,' you consent to our use of cookies. Read more