A CakePHP cheat sheet (CakePHP reference page)

Summary: This is a CakePHP cheat sheet. (Note: This reference was initially created in 2011, and may be slightly out of date.)

As I embark on another CakePHP project after a long hiatus, I'm trying to cram CakePHP back into my head. As part of this effort, I'm creating this large CakePHP cheat sheet (reference page), which I hope will be helpful to the CakePHP community. It is based on both the CakePHP cheat sheet on their Trac website and the CakePHP Cookbook (see the links below).

(Go ahead and say what you will about me, but I happen to like all my CakePHP examples on one big page like this that I can search, rather than spread out across many different pages. That's what a cheat sheet is, right? Also, please note that there are very few ads on this page, besides the standard ads on the side columns.)

CakePHP Default Directory Structure

This is what the CakePHP directory structure looks like by default:


CakePHP Naming Conventions

This is a list of the CakePHP naming conventions, specifically the CakePHP Model, View, and Controller naming conventions.

CakePHP Models

  • class names are singular
  • class names UpperCamelCased
  • filenames use a lower-case underscored syntax
  • database tables are plural underscored
  • set var $name in your model definition (PHP4)

CakePHP Controllers

  • class names are plural
  • class names are UpperCamelCased for multi-word controllers
  • class names also end with 'Controller'
  • file names use a lower-case underscored syntax
  • file names also end with '_controller.php'.

CakePHP Views

  • views are in folders that match controller
  • view folders are plural underscored
  • views are named after actions they display.
  • name the view file after action name, in lowercase.

CakePHP naming conventions - Examples

Assuming we have a database table named orders, the following standard CakePHP naming conventions should be used:

  filename  = order.php
  classname = Order
  directory = app/models

  filename  = (same as the action name in the controller)
  extension = .ctp (the filename extension)
  directory = app/views/orders

  filename  = orders_controller.php
  classname = OrdersController
  directory = app/controllers

Assuming we have a database table named order_items, the following standard CakePHP naming conventions should be used:

  filename  = order_item.php
  classname = OrderItem
  directory = app/models

  filename  = (same as the action name in the controller)
  extension = .ctp (the filename extension)
  directory = app/views/order_items

  filename  = order_items_controller.php
  classname = OrderItemsController
  directory = app/controllers

CakePHP bake Command Examples

Here are some CakePHP bake examples (cake bake examples):

cake bake

cake bake controller
cake bake model
cake bake view
cake bake project

cake bake controller orders
cake bake model order

CakePHP Foreign Key Examples and Relationship Types

From the CakePHP Cookbook, there are four possible CakePHP relationship types:

Relationship    Association Type       Example
one to one      hasOne                 A user has one profile.
one to many     hasMany                A user can have multiple recipes.
many to one     belongsTo              Many recipes belong to a user.
many to many    hasAndBelongsToMany    Recipes have, and belong to many tags.

Further details on these relationships can be found at the CakePHP Cookbook pages:

CakePHP relationship type examples:

# in a Post model class:

# each Post belongs to a User
var $belongsTo = array('User');

var $hasOne ...

# in the User model
var $hasMany = array('Post');

var $hasAndBelongsToMany

The CakePHP recursive attribute

The CakePHP recursive attribute affects how CakePHP retrieves data, in particular when there are associations between database tables. (I normally indicate these associations with foreign key relationships, but I've also seen applications not indicate foreign key relationships in the database, but just deal with them in the software that is written.)

For instance, the CakePHP Cookbook includes an example of a blog database, and this database is built upon in the excellent book, Beginning CakePHP . In this book, blog "posts" are associated with "tags" and "users". As the author of the book explains, when a Post model runs a query that pulls posts from the database, it will (may) also retrieve associated rows from the tags and users database tables. This is where the CakePHP recursive attribute comes in:

The CakePHP recursive attribute tells the model how far to look when pulling associated rows.

The CakePHP recursive attribute can be set to the following integer values, with the following meanings:

Value   Meaning
-1      returns only the current model, and ignores all associations.
 0      returns the current model, plus its owner(s).
 1      returns the current model, its owner(s), plus their associated models.
 2      returns the current model, its owner(s), their associated models,
        and the associated models of any associations.

In a simple controller index() method, the recursive attribute may be used like this:

function index()
  $this->Post->recursive = 0;
  $this->set('posts', $this->paginate);

TODO - add other recursive examples here

CakePHP find Conditions

One of the things you have to get used to when working with CakePHP is the CakePHP find method. This is a collection of CakePHP find examples.

First, a list of possible CakePHP find query parameters:

Name        Default  Description
type        'first'  can be 'all', 'first', or 'list'. determines what type of
                     find operation to perform. (TODO - more info here)

conditions  null     array containing the find (select) conditions as
                     key/value pairs

fields      null     array specifying which fields should be retrieved
                     in the resulting select query

order       null     sql 'order by conditions. field name must be
                     followed by ASC or DESC

page        null     page number, used for paged data

limit       null     a limit on the number of results returned, like
                     'select * from orders limit 20'.

offset      null     sql offset value (i haven't used this query much
                     myself, but i think it refers to skipping X 
                     number of rows returned in a query)

recursive      1     the cakephp recursive value, relating to associated
                     model data

Next, a very simple CakePHP find query that retrieves all records from the Post model (probably not something you'll want to do in a production application):


A CakePHP find query with one condition:

$this->Post->find('all', array('conditions'=>array('User.id'=>5)));

A CakePHP find query with one "not equal to" condition:

$this->Post->find('all', array('conditions'=>array('User.id'=>'<> 5')));

A CakePHP find query with multiple conditions:

# this is a little lame, but i'm trying to avoid dates
$this->Post->find('all', array('conditions'=>array('User.id'=>1, 'Post.id'=>'> 50')));

A CakePHP find query that uses all the find function parameters:

# TODO - i'm not sure that this is right; i think 'fields' is supposed to be an array
                        'order'=>'Post.id ASC',

A CakePHP find query using a date:

# note: you can search for date or datetime fields by enclosing the table's field name
#       in the SQL DATE() function.
$this->Post->find('all', array('conditions'=>array('User.id'=>5, 'DATE(Post.date)'=>'CURDATE()')));

# TODO demonstrate "date >" and "date <" conditions

CakePHP find queries with ORDER BY examples:

array('order'=>'date ASC')
array('order'=>'date DESC')
array('order'=>'User.id DESC')

A collection of other CakePHP find query examples:

These CakePHP find examples are lines of code that would be used in an OrderController class:

$this->Order->find(null, null, 'date DESC');
$this->Order->find('all', array('conditions'=>array('User.id'=>1)));
$this->Order->find('all', array('conditions'=>array('User.id'=>array(1,2,3,4))));
$this->Order->find('all', array('conditions'=>array('User.id'=>'<> 1')));
$this->Order->find('all', array('conditions'=>array('User.id'=>1, 'DATE(Post.date)'=>'CURDATE()')));
$this->Order->find('all', array('order'=>'date ASC', 'limit'=>20, 'recursive'=>0);

Here are some CakePHP find examples from the CakePHP retrieving your data book page:

$params can contain all these:

  'conditions' => array('Model.field' => $thisValue), //array of conditions
  'recursive' => 1, //int
  'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
  'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
  'group' => array('Model.field'), //fields to GROUP BY
  'limit' => n, //int
  'page' => n, //int
  'offset'=>n, //int   
  'callbacks' => true //other possible values are false, 'before', 'after'

Here's a CakePHP function showing several different CakePHP find examples:

# find('first', $params) syntax

function some_function() {
  $this->Article->order = null; // resetting if it's set
  $semiRandomArticle = $this->Article->find();
  $this->Article->order = 'Article.created DESC'; // simulating the model having a default order
  $lastCreated = $this->Article->find();
  $alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));
  $specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));

Here's a CakePHP find count example:

# find('count', $params)

function some_function() {
   $total = $this->Article->find('count');
   $pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
   $authors = $this->Article->User->find('count');
   $publishedAuthors = $this->Article->find('count', array(
      'fields' => 'DISTINCT Article.user_id',
      'conditions' => array('Article.status !=' => 'pending')

Some CakePHP find all examples:

# find('all', $params) syntax

function some_function() {
   $allArticles = $this->Article->find('all');
   $pending = $this->Article->find('all', array('conditions' => array('Article.status' => 'pending')));
   $allAuthors = $this->Article->User->find('all');
   $allPublishedAuthors = $this->Article->User->find('all', array('conditions' => array('Article.status !=' => 'pending')));

CakePHP find list examples, useful for creating select boxes:

# find('list', $params) syntax

function some_function() {
    $allArticles = $this->Article->find('list');
    $pending = $this->Article->find('list', array(
        'conditions' => array('Article.status' => 'pending')
    $allAuthors = $this->Article->User->find('list');
    $allPublishedAuthors = $this->Article->find('list', array(
        'fields' => array('User.id', 'User.name'),
        'conditions' => array('Article.status !=' => 'pending'),
        'recursive' => 0

See the CakePHP retrieving your data book page for many more CakePHP find examples, including:

  • find threaded
  • find neighbors
  • findAllBy
  • findBy
  • query
  • field
  • read
  • More complex CakePHP find examples

CakePHP paginate Examples

Another important CakePHP method to know is the CakePHP paginate method. Here is a collection of CakePHP paginate examples.

A basic CakePHP paginate method in a controller:

function index() {
  $this->Order->recursive = 0;
  $this->set('orders', $this->paginate());

You can also control CakePHP pagination with the CakePHP paginate variable in your CakePHP controllers, like this:

class RecipesController extends AppController {

    var $paginate = array(
        'limit' => 25,
        'order' => array(
            'Post.title' => 'asc'

or this:

class RecipesController extends AppController {

    var $paginate = array(
        'fields' => array('Post.id', 'Post.created'),
        'limit' => 25,        
        'order' => array(
            'Post.title' => 'asc'

For more information on CakePHP pagination, see the CakePHP pagination book page.

CakePHP logging

You can write to CakePHP log files using the CakeLog::write method:

CakeLog::write('debug', 'Something did not work');

Or you can use the CakePHP log function in any class that extends the CakePHP Object class:

$this->log("Something did not work!", 'debug');

You can also perform CakePHP error logging like this:

Configure::write('log', E_WARNING);

CakePHP Global Methods

These are names of some globally available CakePHP methods (assuming you extend the CakePHP Object class):

debug   - use like <? debug($order); ?> in a view
e       - shortcut for echo()
pr      - shortcut for print_r()

CakePHP Global Constants

This is a list of CakePHP global constants. (I don't know much about these yet.)


CakePHP Controller properties, methods, callbacks

CakePHP controller properties:

$name = null
$action = null
$autoLayout = true
$autoRender = true
$base = null
$beforeFilter = null
$cacheAction = false
$components = array()
$data = array()
$helpers = array('Html')
$here = null
$layout = 'default'
$output = null
$pageTitle = false
$params = array()
$persistModel = false
$plugin = null
$uses = false
$view = 'View'
$viewPath = null
$webroot = null
$_viewClass = null
$_viewVars = array()

CakePHP controller methods

cleanUpFields ()
constructClasses ()
flash ($message, $url, $pause = 1)
flashOut ($message, $url, $pause = 1)
generateFieldNames ($data = null, $doCreateOptions = true)
postConditions ($data, $operator = '', $bool = 'AND', $exclusive = false)
redirect ($url, $status = null)
referer ($default = null, $local = false)
render ($action = null, $layout = null, $file = null)
set ($one, $two = null)
setAction ($action)
validate ()
validateErrors ()

CakePHP controller callbacks

afterFilter ()
beforeFilter ()
beforeRender ()

CakePHP Model properties, methods, callbacks, and validation

CakePHP Model properties:

$belongsTo = array()
$cacheQueries = true
$data = array()
$displayField = null
$hasAndBelongsToMany = array()
$hasMany = array()
$hasOne = array()
$id = false
$logTransactions = false
$name = null
$primaryKey = null
$recursive = 1
$useDbConfig = 'default'
$useTable = null
$validate = array()
$validationErrors = array()

CakePHP Model methods:

bindModel ($params)
create ()
delete ($id = null, $cascade = true)
escapeField ($field)
execute ($data)
exists ()
field ($name, $conditions = null, $order = null)
find ($conditions = null, $fields = null, $order = null, $recursive = null)
findAll ($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null)
findAllThreaded ($conditions = null, $fields = null, $sort = null)
findCount ($conditions = null, $recursive = 0)
findNeighbours ($conditions = null, $field, $value)
generateList ($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null)
getAffectedRows ()
getColumnType ($column)
getColumnTypes ()
getDisplayField ()
getID ($list=0)
getLastInsertID ()
getNumRows ()
hasAny ($conditions = null)
hasField ($name)
invalidate ($field)
invalidFields ($data = array())
isForeignKey ($field)
loadInfo ()
query ()
read ($fields = null, $id = null)
remove ($id = null, $cascade = true)
save ($data = null, $validate = true, $fieldList = array())
saveField ($name, $value, $validate = false)
set ($one, $two = null)
setDataSource ($dataSource = null)
setSource ($tableName)
unbindModel ($params)
validates ($data=array())
setSource ($tableName)

CakePHP Model callbacks

afterDelete ()
afterFind ($results)
afterSave ()
beforeDelete ()
beforeFind (&$queryData)
beforeSave ()
beforeValidate ()

CakePHP Model validation


CakePHP View properties and methods

CakePHP View properties

$action = null
$autoLayout = true
$autoRender = true
$base = null
$controller = null
$ext = '.thtml'
$hasRendered = null
$helpers = array('Html')
$here = null
$layout = 'default'
$loaded = array()
$models = array()
$name = null
$pageTitle = false
$parent = null
$plugin = null
$subDir = null
$themeWeb = null
$uses = false

CakePHP View methods

element ($name)
error ($code, $name, $message)
pluginView ($action, $layout)
render ($action = null, $layout = null, $file = null)
renderCache ($filename, $timeStart)
renderElement ($name, $params = array())
renderLayout ($content_for_layout)
setLayout ($layout)

CakePHP Data Validation

Here are some CakePHP data validation examples, mostly from the CakePHP Data Validation book page:

class User extends AppModel {
    var $name = 'User';
    var $validate = array(
        'login' => 'alphaNumeric',
        'email' => 'email',
        'born' => 'date'

CakePHP Helpers

CakePHP Html Helper

addCrumb ($name, $link)
charset ($charset, $return = false)
checkbox ($fieldName, $title = null, $htmlAttributes = null, $return = false)
css ($path, $rel = 'stylesheet', $htmlAttributes = null, $return = false)
dateTimeOptionTag ($tagName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
dayOptionTag ($tagName, $value = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
file ($fieldName, $htmlAttributes = null, $return = false)
formTag ($target = null, $type = 'post', $htmlAttributes = null)
getCrumbs ($separator = '»', $startText = false, $return = false)
guiListTree ($data, $htmlAttributes = null, $bodyKey = 'body', $childrenKey = 'children', $return = false)
hidden ($fieldName, $htmlAttributes = null, $return = false)
hourOptionTag ($tagName, $value = null, $format24Hours = false, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
image ($path, $htmlAttributes = null, $return = false)
input ($fieldName, $htmlAttributes = null, $return = false)
link ($title, $url = null, $htmlAttributes = null, $confirmMessage = false, $escapeTitle = true, $return = false)
linkEmail ($title, $email = null, $options = null)
meridianOptionTag ($tagName, $value = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
minuteOptionTag ($tagName, $value = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
monthOptionTag ($tagName, $value = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
parseHtmlOptions ($options, $exclude = null, $insertBefore = ' ', $insertAfter = null)
password ($fieldName, $htmlAttributes = null, $return = false)
radio ($fieldName, $options, $inbetween = null, $htmlAttributes = array(), $return = false)
selectTag ($fieldName, $optionElements, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true, $return = false)
setFormTag ($tagValue)
submit ($caption = 'Submit', $htmlAttributes = null, $return = false)
tableCells ($data, $oddTrOptions = null, $evenTrOptions = null, $return = false)
tableHeaders ($names, $trOptions = null, $thOptions = null, $return = false)
tagErrorMsg ($field, $text)
tagIsInvalid ($model, $field)
tagValue ($fieldName)
textarea ($fieldName, $htmlAttributes = null, $return = false)
trim ()
url ($url = null, $return = false)
validate ()
validateErrors ()
yearOptionTag ($tagName, $value = null, $minYear = null, $maxYear = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true)
_parseAttributes ($options, $exclude = null, $insertBefore = ' ', $insertAfter = null)

CakePHP Form Helper

button ($params, $type= 'button', $options=array())
create ($model=null, $options=array())
dateTime ($tagName, $dateFormat= 'DMY', $timeFormat= '12', $selected=null, $attributes=array(), $showEmpty=true)
day ($fieldName, $selected=null, $attributes=array(), $showEmpty=true)
end ($options=null)
error ($field, $text=null, $options=array())
file ($fieldName, $options=array())
hidden ($fieldName, $options=array())
hour ($tagName, $format24Hours=false, $selected=null, $attributes=array(), $showEmpty=true)
input ($tagName, $options=array())
inputs ($fields=null, $blacklist=null)
isFieldError ($field)
label ($tagName=null, $text=null, $attributes=array())
meridian ($tagName, $selected=null, $attributes=array(), $showEmpty=true)
minute ($tagName, $selected=null, $attributes=array(), $showEmpty=true)
month ($tagName, $selected=null, $attributes=array(), $showEmpty=true)
password ($fieldName, $options=array())
secure ($fields)
select ($fieldName, $options=array(), $selected=null, $attributes=array(), $showEmpty= '')
submit ($caption= 'Submit', $options=array())
submitImage ($path, $options=array())
text ($fieldName, $options=array())
textarea ($fieldName, $options=array())
year ($fieldName, $minYear=null, $maxYear=null, $selected=null, $attributes=array(), $showEmpty=true)

CakePHP Ajax Helper

autoComplete ($field, $url="", $options=array())
div ($id, $options=array())
divEnd ($id)
drag ($id, $options=array())
drop ($id, $options=array())
dropRemote ($id, $options=array(), $ajaxOptions=array())
editor ($id, $url, $options=array())
form ($params=null, $type= 'post', $options=array())
isAjax ()
link ($title, $href=null, $options=array(), $confirm=null, $escapeTitle=true)
observeField ($field_id, $options=array())
observeForm ($field_id, $options=array())
remoteFunction ($options=null)
remoteTimer ($options=null)
slider ($id, $track_id, $options=array())
sortable ($id, $options=array())
submit ($title= 'Submit', $options=array())

CakePHP Text Helper

highlight ($text, $phrase, $highlighter= '< span class="highlight">\1</span >')
stripLinks ($text)
autoLinkUrls ($text, $htmlOptions = array())
autoLinkEmails ($text, $htmlOptions = array())
autoLink ($text, $htmlOptions = array())
truncate ($text, $length, $ending = '...', $exact = true)
trim ()
excerpt ($text, $phrase, $radius = 100, $ending = "...")
flay ($text, $allowHtml = false)

CakePHP Time Helper

dayAsSql ($date_string, $field_name, $return = false)
daysAsSql ($begin, $end, $field_name, $return = false)
fromString ($date_string)
isThisYear ($date_string, $return = false)
isToday ($date_string, $return = false)
isTomorrow ($date_string, $return = false)
nice ($date_string=null, $return = false)
niceShort ($date_string=null, $return = false)
relativeTime ($datetime_string, $format = 'j/n/y', $return = false)
timeAgoInWords ($datetime_string, $format = 'j/n/y', $backwards = false, $return = false)
toAtom ($date_string, $return = false)
toRSS ($date_string, $return = false)
toUnix ($date_string, $return = false)
trim ($string, $length, $ending = '..')
wasWithinLast ($timeInterval, $date_string, $return = false)
wasYesterday ($date_string, $return = false)

CakePHP Number Helper

precision ($number, $precision = 3)
toReadableSize ($size)
toPercentage ($number, $precision = 2)

CakePHP Components

CakePHP Session Component

check ($name)
del ($name)
delete ($name)
error ()
flash ($key)
read ($name)
renew ()
setFlash ($flashMessage, $layout = 'default', $param = null, $key = 'flash')
valid ()
write($name, $value)

CakePHP RequestHandler Component

accepts ($types)
getAjaxVersion ()
getClientIP ()
getReferer ()
isAjax ()
isAtom ()
isDelete ()
isGet ()
isMobile ()
isPost ()
isPut ()
isRss ()
isXml ()
setContent ($name, $type)
stripAll ($string)
stripImages ($string)
stripScripts ($string)

CakePHP Security Component

requirePost ([$action1, $ action2, $action3, ...])
requireAuth ([$action1, $ action2, $action3, ...])

CakePHP ACL Component

check ($aro, $aco, $action="*")
allow ($aro, $aco, $action="*")
deny ($aro, $aco, $action="*")
inherit ($aro, $aco, $action="*")
grant ($aro, $aco, $action="*")
revoke ($aro, $aco, $action="*")
getAro ($id)
getAco ($id)

CakePHP SQL Debug

To add CakePHP SQL debug output in CakePHP 1.3, change the CakePHP debug config parameter to '2' in this line of the $app/config/core.php file:

Configure::write('debug', 2);

Then add this line to the bottom of your CakePHP view page:

echo $this->element('sql_dump');

Other CakePHP tutorials

For deeper problems, I've created some other CakePHP tutorials:

CakePHP API Documentation

Links to some of the most important (common) CakePHP classes:

CakePHP - TODO Items

This is a list of more things I want to add to this CakePHP cheat sheet:

  • Auth information
  • AppController
  • var $helpers = array('Html', 'Form', 'Ajax', 'Javascript');  // in AppController or other controllers
  • More find queries
  • read()
  • save()
  • Date, time, calendar, currency
  • setFlash
  • redirect
  • Validation
  • $tags = $this->Post->Tag->find('list');
  • $users = $this->Post->User->find('list');
  • $tags = $this->compact(...)

CakePHP support URLs

Here is a list of CakePHP support URLs:

This page is sponsored by Valley Programming - Alaska PHP computer programming services

want to sponsor a page? learn more.

CakePHP cheat sheet (reference page) - Summary

I hope this CakePHP cheat sheet has been helpful. As mentioned, it is very heavily based on the CakePHP reference page, which can be found at the CakePHP Trac website. Some of the examples also come from the excellent Beginning CakePHP book from apress ('author'=>'David Golding').

(FWIW, yes, I feel guilty about copying their CakePHP cheat sheet content, but I hope to vastly improve on it over time as I continue to develop my own CakePHP applications. I really like my cheat sheets to have a lot of examples I can easily reference, which is something I think their CakePHP cheat sheet is lacking.)