cakephp

CakePHP is a PHP web framework.

Abstractions and interfaces

Routing

URL: foo/do_something -> Controller: class FooController, method do_something()

file system:

plays a role in routing; routing mechanism uses fs layout to find and load classes

controllers/ components/ foo_controller.php bar_controller.php models behaviors/ foo.php bar.php views/ helpers/ foo/ do_something.tmpl bar/

Actions

request

$this->params['named'] - map; named GET params in the form /foo/view/var1:3/var2:4 $this->params['url'] - map; current url + GET params in the form /foo/view/?var1=3&var2=4 $this->data - map; POST params $this->postConditions() - convert POST params to a set of conditions for model find() method

model layer

$this->loadModel('Foo') - load model Foo $this->Foo - model layer access $this->paginate() - shortcut functions that examines GET params and paginates model results accordingly

controller layer

$this->beforeFilter() - called before every action in the controller $this->beforeRender() - called after controller action logic $this->afterFilter() - called after rendering is complete

view layer

$this->set(string $var, mixed $value) - set view variables

response

$this->render(string $action, string $layout, string $file) - render the view $this->redirect(mixed $url, integer $status, boolean $exit)

Models

retrieval

Foo->find('first') Foo->find('count') Foo->find('all') Foo->find('list') - flat list, for things like select boxes Foo->query(string $query) - run an SQL query, with results similar to find('all')

saving data

Foo->save(array $data) - save data to db. creating or updating is controlled by the model's id field. Foo->del(int $id) - delete record identified by id Foo->deleteAll(mixed $conditions) - delete records using a set of conditions

Views

Loading models in helpers

<?php
    if (ClassRegistry::isKeySet('User')) {
        $usrObject = &ClassRegistry::getObject('User');
    }
?>

Bugs/gotchas

Class naming

loadModel loads an AppModel instance instead of a Gadget instance.

<?php
    class Gadget extends AppModel {}

    class Belt extends AppModel {
        var $hasMany = array(
            'Widget' => array(
                'class' => 'Gadget',
                'foreignKey' => 'belt_id',
            ),
        );
    }

    class BeltsController extends AppController {
        function index() {
            $this->loadModel('Gadget');
            // and now $this->Gadget is an AppModel instance. Why?
        }
    }
?>