{309} - 3.7.8 Model Attributes

For a complete list of model attributes and their descriptions visit the CakePHP API. Check out http://api.cakephp.org/1.2/classModel.html.

$useDbConfig

The name of the database configuration this model uses. Configurations are specified in /app/config/database.php as class variables. Defaults to ‘default.’

$useTable

The name of the database table this model uses. The default table used is the lower-cased, plural version of the model’s classname. Set this attribute to the name of an alternate table, or set it to false if you wish the model to have no database relation at all.

$primaryKey

If the model is tied to a database table, this attribute holds the name of the column of the tables primary key. Defaults to ‘id’.

$displayField

This attribute is used to define the field the model will use for display in scaffolding elements. When associated data is presented (i.e., which Ingredient you’d like to associate to a Recipe) scaffolding will use the row’s ID, ‘name’, or ‘title’ fields unless $displayField has been specified.

$recursive

An integer specifying the number of levels you wish CakePHP to fetch associated model data in find() and findAll() operations.

Imagine your application features Groups which have many Users which in turn have many Articles. You can set $recursive to different values based on the amount of data you want back from a $this->Group->find() call:

  • 0 - Cake fetches Group data
  • 1 - Cake fetches a Group and its associated Users
  • 2 - Cake fetches a Group, its associated Users, and the Users' associated Articles

Set it no higher than you need–having CakePHP fetch data you aren’t going to use unnecessarily slows your app.

$order

The default ordering of data for any find operation. Possible values include:

$order = "field"
$order = "Model.field";
$order = "Model.field asc";
$order = "Model.field ASC";
$order = "Model.field DESC";
$order = array("Model.field" => "asc", "Model.field2" => "DESC");
  1. $order = "field"
  2. $order = "Model.field";
  3. $order = "Model.field asc";
  4. $order = "Model.field ASC";
  5. $order = "Model.field DESC";
  6. $order = array("Model.field" => "asc", "Model.field2" => "DESC");

Value of the primary key ID of the record that this model is currently pointing to. If you are saving model data inside of a loop, set $id to null ($this->ModelName->id = null) in order to ready the model class for the next set of data.

$data

The container for the model’s fetched data. While data returned from a model class is normally used as returned from a find() call, you may need to access information stored in $data inside of model callbacks.

$_schema

Contains metadata describing the model’s database table fields. Each field is described by:

  • name
  • type (integer, string, datetime, etc.)
  • null
  • default value
  • length
$validate

This attribute holds rules that allow the model to make data validation decisions before saving. Keys named after fields hold regex values allowing the model to try to make matches.

For more information on validation, see the Data Validation chapter later on in this manual.

$name

The name of this model. CakePHP model names are CamelCased and singular. CakePHP users on PHP4 should always declare $name in order to avoid the quirks involved with PHP4 class-naming.

$cacheQueries

If set to true, data fetched by the model during a single request is cached. This caching is in-memory only, and only lasts for the duration of the request. Any duplicate requests for the same data is handled by the cache.

$belongsTo, $hasOne, $hasMany, $hasAndBelongsToMany

These model attributes will be covered later in this chapter. They hold arrays that define how this model is related to others.

$actsAs

This attribute is also covered later on. The $actsAs attribute stores the names of Behaviors this model uses.

{2105} - 3.7.8 Model Attributes

Model attributes allow you to set properties that can override the default model behavior.

For a complete list of model attributes and their descriptions visit the CakePHP API. Check out http://api.cakephp.org/1.2/class_model.html.

Differences

Lines: 1-123Lines: 1-5
 <title>Model Attributes</title> <title>Model Attributes</title>
-<p>
For a complete list of m
odel attributes and their descriptions visit the CakePHP API. Check out &lt;a href="http://api.cakephp.org/1.2/classModel.html">http://api.cakephp.org/1.2/classModel.html</a>.
</
p>
<div class="m
ethod">r /> $useDbConfig
&l
t;/div>
<p>
Th
e name of the database configuration this model uses. Configurations are specified in /app/config/database.php as class variables. Defaults to ‘default.’
</p>
<di
v class="method">r /> $useTabler /></div>
<p>
The name of the
database table this model uses. The default table used is the lower-cased, plural version of the model’s classname. Set this attribute to the name of an alternate table, or set it to <em>false</em> if you wish the model to have no database relation at all.
</p>
<div class="method">
$primaryKey
</div>

<p>r /> If the model is tied to a database table, this attribute holds the name of the column of the tables primary key. Defaults to ‘id’.
</p>
<div class="m
ethod">
$disp
layField
</div>
<p>
This
attribute is used to define the field the model will use for display in scaffolding elements. When associated data is presented (i.e., which Ingredient you’d like to associate to a Recipe) scaffolding will use the row’s ID, ‘name’, or ‘title’ fields unless $displayField has been specified.
&l
t;/p>
<d
iv class=&quot;method">
$recur
sive /></div>
<p>
An
integer specifying the number of levels you wish CakePHP to fetch associated model data in find() and findAll() operations.
</p>
<p>
Imagine your application features Groups which have many Users which in turn have many Articles. You can set $recursive to different values based on the amount of data you want back from a $this-&gt;Group-&gt;find() call:
</p>
<ul>
<li>0 - C
ake fetches Group data
</li>
<li>1 - Cak
e fetches a Group and its associated Users
</li>
<li>2 - Cake fetches a Group, its associated Users, and the Users' associated Articles
</li>
</ul>
<p>
Set it no higher than you need–having CakePHP fetch data you aren’t going to use unnecessarily slows your app.
</p>
<div class
="method&quot;&gt;
$order
</div>
<
p>
The default ordering of data for any find operation. Possible values include
:
/></p>
<pre>
$order = "f
ield"
$order = "Model
.field";
$order = "Model.field as
c";
$order = "Model.field ASC";
$order = "Model.field DESC";
$order =
array("Model.field" =&amp;gt; "asc", "Model.field2" =&gt; "DESC");
</pre>
<p>
Value of t
he primary key ID of the record that this model is currently pointing to. If you are saving model data inside of a loop, set $id to null ($this-&gt;ModelName-&gt;id = null) in order to ready the model class for the next set of data.
/></p>
<div
class="method">
$data
</div>
<p>
The container for the
model’s fetched data. While data returned from a model class is normally used as returned from a find() call, you may need to access information stored in $data inside of model callbacks.
</p>
<div class="method
">
$_sc
hema
&l
t;/div&gt;
<
p>
Contains metadata describing the model’s database table fields. Each field is described by
:
/></p>
<ul>
<li>n
ame
</li>
<li>ty
pe (integer, string, datetime, etc.)
</li>
<li>null
</li>
<li>default value
</li>
<li>length
</li>
</ul>
<div
class="method">
$validate
</div>
<p>
This attribute holds rules that allow the model to m
ake data validation decisions before saving. Keys named after fields hold regex values allowing the model to try to make matches.
</
p>
<p>
For more information on validation, see t
he Data Validation chapter later on in this manual.
</p>
<div class=&qu
ot;method">r /> $name
</div&
gt;
/><p>
The name of this model
. CakePHP model names are CamelCased and singular. CakePHP users on PHP4 should always declare $name in order to avoid the quirks involved with PHP4 class-naming.
/></p>
<div
class="method">
$cach
eQueries
&
lt;/div>
<p>
If set to true, data fetched by the model during a single request is cached
. This caching is in-memory only, and only lasts for the duration of the request. Any duplicate requests for the same data is handled by the cache.
</p>
<div cl
ass="method">
$belongsTo, $hasOne, $hasMany, $hasAndBelongsToMany
</div>
<p>
These model attributes will be covered later in this chapter
. They hold arrays that define how this model is related to others.
</p>
<div class="method">
$actsAs
</div>
<p>
This attribute is also covered later on. The $actsAs attribute stores the names of Behaviors this model uses.
</p>
+<p>Model attributes allow you to set properties that can override the default model behavior.</p>

<p>For a complete list of model attributes and their descriptions visit the CakePHP API. Check out <a href="http://api.cakephp.org/1.2/class_model.html">http://api.cakephp.org/1.2/class_model.html</a>.</p>