Welcome to the Cookbook

loading...

3.7.8 Model Attributes

There is no translation yet for this section. Please help out and translate this.. More information about translations

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/class/model.

3.7.8.1 useDbConfig

There is no translation yet for this section. Please help out and translate this.. More information about translations

The useDbConfig property is a string that specifies the name of the database connection to use to bind your model class to the related database table. You can set it to any of the database connections defined within your database configuration file. The database configuration file is stored in /app/config/database.php.

The useDbConfig property is defaulted to the 'default' database connection.

Example usage:

class Example extends AppModel {
   var $useDbConfig = 'alternate';
}
  1. class Example extends AppModel {
  2. var $useDbConfig = 'alternate';
  3. }

3.7.8.2 useTable

There is no translation yet for this section. Please help out and translate this.. More information about translations

The useTable property specifies the database table name. By default, the model uses the lowercase, plural form of the model's class name. Set this attribute to the name of an alternate table, or set it to false if you wish the model to use no database table.

Example usage:

class Example extends AppModel {
   var $useTable = false; // This model does not use a database table
}
  1. class Example extends AppModel {
  2. var $useTable = false; // This model does not use a database table
  3. }

Alternatively:

class Example extends AppModel {
   var $useTable = 'exmp'; // This model uses a database table 'exmp'
}
  1. class Example extends AppModel {
  2. var $useTable = 'exmp'; // This model uses a database table 'exmp'
  3. }

3.7.8.3 tablePrefix

There is no translation yet for this section. Please help out and translate this.. More information about translations

The name of the table prefix used for the model. The table prefix is initially set in the database connection file at /app/config/database.php. The default is no prefix. You can override the default by setting the tablePrefix attribute in the model.

Example usage:

class Example extends AppModel {
   var $tablePrefix = 'alternate_'; // will look for 'alternate_examples'
}
  1. class Example extends AppModel {
  2. var $tablePrefix = 'alternate_'; // will look for 'alternate_examples'
  3. }

3.7.8.4 primaryKey

There is no translation yet for this section. Please help out and translate this.. More information about translations

Each table normally has a primary key, id. You may change which field name the model uses as its primary key. This is common when setting CakePHP to use an existing database table.

Example usage:

class Example extends AppModel {
    var $primaryKey = 'example_id'; // example_id is the field name in the database
}
  1. class Example extends AppModel {
  2. var $primaryKey = 'example_id'; // example_id is the field name in the database
  3. }

3.7.8.5 displayField

There is no translation yet for this section. Please help out and translate this.. More information about translations

The displayField attribute specifies which database field should be used as a label for the record. The label is used in scaffolding and in find('list') calls. The model will use name or title, by default.

For example, to use the username field:

class User extends AppModel {
   var $displayField = 'username';
}
  1. class User extends AppModel {
  2. var $displayField = 'username';
  3. }

Multiple field names cannot be combined into a single display field. For example, you cannot specify, array('first_name', 'last_name') as the display field.

3.7.8.6 recursive

There is no translation yet for this section. Please help out and translate this.. More information about translations

The recursive property defines how deep CakePHP should go to fetch associated model data via find(), findAll() and read() methods.

Imagine your application features Groups which belong to a domain and 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:

DepthDescription
-1Cake fetches Group data only, no joins.
0Cake fetches Group data and its domain
1Cake fetches a Group, its domain and its associated Users
2Cake fetches a Group, its domain, 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 slows your app unnecessarily. Also note that the default recursive level is 1.

If you want to combine $recursive with the fields functionality, you will have to add the columns containing the required foreign keys to the fields array manually. In the example above, this could mean adding domain_id.

3.7.8.7 order

There is no translation yet for this section. Please help out and translate this.. More information about translations

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");

3.7.8.8 data

There is no translation yet for this section. Please help out and translate this.. More information about translations

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.

3.7.8.9 _schema

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

  • name
  • type (integer, string, datetime, etc.)
  • null
  • default value
  • length
Example Usage:
var $_schema = array(
	'first_name' => array(
		'type' => 'string', 
		'length' => 30
	),
	'last_name' => array(
		'type' => 'string', 
		'length' => 30
	),
	'email' => array(
		'type' => 'string',
		'length' => 30
	),
	'message' => array('type' => 'text')
);
  1. var $_schema = array(
  2. 'first_name' => array(
  3. 'type' => 'string',
  4. 'length' => 30
  5. ),
  6. 'last_name' => array(
  7. 'type' => 'string',
  8. 'length' => 30
  9. ),
  10. 'email' => array(
  11. 'type' => 'string',
  12. 'length' => 30
  13. ),
  14. 'message' => array('type' => 'text')
  15. );

3.7.8.10 validate

There is no translation yet for this section. Please help out and translate this.. More information about translations

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.

It is not necessary to call validate() before save() as save() will automatically validate your data before actually saving.

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

3.7.8.11 name

There is no translation yet for this section. Please help out and translate this.. More information about translations

As you saw earlier in this chapter, the name attribute is a compatibility feature for PHP4 users and is set to the same value as the model name.

Example usage:

class Example extends AppModel {
   var $name = 'Example';
}
  1. class Example extends AppModel {
  2. var $name = 'Example';
  3. }

3.7.8.12 cacheQueries

There is no translation yet for this section. Please help out and translate this.. More information about translations

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.