Model
In the MVC methodology, the model is a representation of an Active Record (an abstraction of a database table with methods to perform actions on that data). You will make model classes for each of your tables in your Cake application.
The Model class extends Object. Model is inherited by AppModel.
The model contains a number of features:
Public Variables
- $id sets the primary key value
- $primaryKey sets the field name for this model’s primary key (in the database)
- $useDbConfig - sets which database configuration set to use (defined in
/app/config/database.php) - $useTable sets the table for the model to use, overrides default behaviour
- $validate sets the validation for the model
- $tablePrefix appends a prefix to the table name
- $recursive sets the number of “levels of association” this Model is to fetch on find and findAll calls (not in read()) (added in Nightlies as of now)
Association Variables
Public Methods
- read returns an array of field values from the table using the ID of the Active Record
- save saves the data from the Active Record to the table
- validates returns true if the supplied data validates
- validationErrors returns the errors from validation as an array
- getLastInsertID returns the id number of the last inserted record
- find Finds a single matching record based on the supplied criteria
- findAll Finds a set of records matching a supplied criteria
- findBySql Finds a set of records based on a sql query criteria (deprecated since RC1)
- findCount Returns number of rows matching given SQL condition
- findNeighbours Returns an array with keys “prev” and “next” that holds the id’s of neighbouring data, which is useful when creating paged lists.
- hasAny Returns true if a record that meets given conditions exists
- beforeFind Callback
- beforeSave Callback
- afterSave Callback
- beforeDelete Callback
- afterDelete Callback
- beforeValidate Callback
Additional, "magic" methods
Assume a Contact model with a field called first_name:
$this->Contact->findByFirstName('Nate'); $this->Contact->findAllByFirstName('Nate');
Also
$this->Contact->read(null, $id);
Reads the contact record of primaryKey $id.
Stuff that can bite you
(Note that as this seems to be solved as stated in the next section: searching_using_is_null_is_not_null_like_etc. — Stefan 2006/06/12 10:34) Here are examples of CakePHP features that are still not finished.
Finding all Projects that Bobby Ewing is doing, where the Project hasMany Contractor.
$data = $this->Project->findAll("Contractor.name like '%Bobby Ewing%'"); // Will not work now
Searching using IS NULL, IS NOT NULL, LIKE % etc
all credits to jtreglos
If you need to use Model::find() or Model::findAll() or Model::generateList() and all that methods that use an array of conditions you can write something like the following example.
I think this tips can address also the above statement on stuff_that_can_bite_you
$data=$this->Post->generateList(array($this->Task->escapeField('deleted').' IS NULL', 'author'=>'Tom')); $data=$this->Post->generateList(array($this->Task->escapeField('title')." Like %'".$part_of_title."%', 'author'=>'Tom'));
— Stefan 2006/06/12 10:32
Ordering a search when an association is set
If you want to order a search with recursive set to 1 or more, add the model name to each field: (and not the table name)
$this->Actu->recursive = 1; $this->set('actus', $this->Actu->findAll("","","Actu.created DESC"));
— Fraisouille 2006/07/22 06:50