Random CakePHP Tips

recipes from around the world

As I find more and more tips, I'll organize this page.

Inserting data

create() prepares your model for insertion each time. Otherwise once you use the model to save data it sets the id of that model and then each subsequent save will be an update instead of an insert.

Model-less controller

var $uses = null;
Explicitly set $uses to null if there is no model for your controller. Otherwise you'll get a model not found error.

Setting page title and heading

<h2><?php echo $this->pageTitle="Page title"; ?></h2>

Including/Requiring files

require_once(WWW_ROOT . DS . 'files' . 'includeme.txt');
When using include/require, use WWW_ROOT constant to get the correct path.

Automatic Model Name

To get the model name while you're in a view.

Selected Option

$html->selectTag('Dealer/cars', array('audi' => 'Audi TT'), 'audi');
arg1 = select tag name arg2 = array of options - value => text arg3 (optional) = selected option value For setting the 'selected' option for a select tag.

LIKE clause

$this->Customer->findAll(array('address' => "LIKE %{$searchBy}%"));

Missing Controller

You can either leave the default 404 message or default to your homepage (or some other page. To do that create an error.php file in your app directory:
 * error.php
 * custom error handler to redirect invalid urls to homepage

class AppError extends ErrorHandler

    function __construct($method, $messages) {
        static $__previousError = null;
        $this->__dispatch =& new Dispatcher();

        if ($__previousError != array($method, $messages)) {
            $__previousError = array($method, $messages);

            if (!class_exists('AppController')) {

            $this->controller =& new AppController();

            if (method_exists($this->controller, 'apperror')) {
                return $this->controller->appError($method, $messages);
        } else {
            $this->controller =& new Controller();

        call_user_func_array(array(&$this, $method), $messages);

    function missingController($params) {


Add your own or Comment/Correct

Have a tip you'd like to share? Add it here. I'll take a look and if it's not something found in the manual or fully documented in the API I'll add it to this page. Thanks!