A CakePHP Referencia

download A CakePHP Referencia

of 27

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of A CakePHP Referencia

A CakePHP cheat sheet (CakePHP reference page)Submitted by alvin on December 9, 2010 - 8:37pm tags:

cakephp cheat sheet controller find logging model paginate php reference view

A CakePHP cheat sheet: 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 StructureThis is what the CakePHP directory structure looks like by default:

/app app_controller.php app_model.php /config acl.ini.php bootstrap.php core.php databases.php inflections.php routes.php /controllers

/components your_components_here.php your_controllers_here.php /locale /eng /models /behaviors your_behaviors_here.php your_models_here.php /plugins /tests /cases /behaviors /components /controllers /helpers /models /fixtures /groups /tmp /vendors /views /helpers your_views_here.ctp /webroot /css /files /js /cake cake_core_files_are_in_here /docs release_notes_here /vendors

CakePHP Naming ConventionsThis 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:

Model filename = order.php classname = Order directory = app/models View filename = (same as the action name in the controller) extension = .ctp (the filename extension) directory = app/views/orders Controller 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:

Model filename = order_item.php classname = OrderItem directory = app/models View filename = (same as the action name in the controller) extension = .ctp (the filename extension) directory = app/views/order_items Controller filename = order_items_controller.php classname = OrderItemsController directory = app/controllers

CakePHP bake Command ExamplesHere 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 TypesFrom the CakePHP Cookbook, there are four possible CakePHP relationship types:

Relationship one to one profile.

Association Type hasOne

Example A user has one

one to many many to one a user. many to many

hasMany belongsTo hasAndBelongsToMany

A user can have Many recipes belong to Recipes have, and

multiple recipes.

belong to many tags.Further details on these relationships can be found at the CakePHP Cookbook pages:

hasOne belongsTo hasMany hasAndBelongsToMany

CakePHP relationship type examples:

# in a Post model class: # each Post belongs to a User var $belongsTo = array('User'); # TODO var $hasOne ... # in the User model var $hasMany = array('Post'); # TODO var $hasAndBelongsToMany

The CakePHP recursive attributeThe 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 -1 0 1 2

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


associated models. associated models,

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 ConditionsOne 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 type

Default 'first'

Description can be 'all', 'first', or 'list'. find operation to perform. (TODO - more

determines what type of info here)



array containing the find (select) key/value pairs

conditions as

fields retrieved


array specifying which fields should be in the resulting select query

order must be


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

page limit

null null

page number, used for paged data a limit on the number of results 'select * from orders limit 20'.

returned, like

offset query much


sql offset value (i haven't used this myself, but i think it refers to

skipping X number of rows returned in a query) recursive associated model dataNext, 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):


the cakephp recursive value, relating to

$this->Post->find('all');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 $this->Post->find('all', array('conditions'=>array('User.id'=>5), 'fields'=>'Post.name', 'order'=>'Post.id ASC', 'limit'=>20, 'recursive'=>0));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