Model Class Reference

Public Member Functions | |
| _clearCache ($type=null) | |
| _deleteDependent ($id, $cascade) | |
| _deleteLinks ($id) | |
| afterDelete () | |
| afterFind ($results, $primary=false) | |
| afterSave ($created) | |
| beforeDelete ($cascade=true) | |
| beforeFind ($queryData) | |
| beforeSave () | |
| beforeValidate () | |
| bind ($model, $options=array(), $permanent=true) | |
| bindModel ($params, $reset=true) | |
| create ($data=array()) | |
| deconstruct ($field, $data) | |
| del ($id=null, $cascade=true) | |
| delete ($id=null, $cascade=true) | |
| deleteAll ($conditions, $cascade=true, $callbacks=false) | |
| escapeField ($field=null, $alias=null) | |
| execute ($data) | |
| exists ($reset=false) | |
| field ($name, $conditions=null, $order=null) | |
| find ($conditions=null, $fields=array(), $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, $groupPath=null) | |
| getAffectedRows () | |
| getAssociated ($type=null) | |
| getColumnType ($column) | |
| getColumnTypes () | |
| & | getDataSource () |
| getDisplayField () | |
| getID ($list=0) | |
| getInsertID () | |
| getLastInsertID () | |
| getNumRows () | |
| hasAny ($conditions=null) | |
| hasField ($name) | |
| invalidate ($field, $value=null) | |
| invalidFields ($data=array()) | |
| isForeignKey ($field) | |
| isUnique ($fields, $or=true) | |
| joinModel ($assoc, $keys=array()) | |
| loadInfo ($clear=false) | |
| normalizeFindParams ($type, $data, $altType=null, $r=array(), $_this=null) | |
| onError () | |
| query () | |
| read ($fields=null, $id=null) | |
| remove ($id=null, $cascade=true) | |
| save ($data=null, $validate=true, $fieldList=array()) | |
| saveAll ($data=null, $options=array()) | |
| saveField ($name, $value, $validate=false) | |
| schema ($clear=false) | |
| set ($one, $two=null) | |
| setDataSource ($dataSource=null) | |
| setInsertID ($id) | |
| setSource ($tableName) | |
| unbindModel ($params, $reset=true) | |
| updateAll ($fields, $conditions=true) | |
| updateCounterCache ($keys=array()) | |
| validates ($data=array()) | |
Public Attributes | |
| $_schema = null | |
| $actsAs = null | |
| $alias = null | |
| $behaviors = array() | |
| $belongsTo = array() | |
| $cacheQueries = false | |
| $cacheSources = true | |
| $data = array() | |
| $displayField = null | |
| $findQueryType = null | |
| $hasAndBelongsToMany = array() | |
| $hasMany = array() | |
| $hasOne = array() | |
| $id = false | |
| $logTransactions = false | |
| $name = null | |
| $order = null | |
| $primaryKey = null | |
| $recursive = 1 | |
| $table = false | |
| $tablePrefix = null | |
| $tableToModel = array() | |
| $transactional = false | |
| $useDbConfig = 'default' | |
| $useTable = null | |
| $validate = array() | |
| $validationErrors = array() | |
| $whitelist = array() | |
Detailed Description
Definition at line 45 of file model.php.
Member Function Documentation
| Model::_clearCache | ( | $ | type = null |
) |
Private method. Clears cache for this model
- Parameters:
-
string $type If null this deletes cached views if Cache.check is true Will be used to allow deleting query cache also
- Returns:
- boolean true on delete protected
Definition at line 2538 of file model.php.
References clearCache(), Inflector::pluralize(), and Configure::read().
| Model::_deleteDependent | ( | $ | id, | |
| $ | cascade | |||
| ) |
Cascades model deletes to hasMany and hasOne relationships.
- Parameters:
-
string $id ID of record that was deleted boolean $cascade Set to true to delete records that depend on this record protected
Definition at line 1503 of file model.php.
Referenced by del(), and deleteAll().
| Model::_deleteLinks | ( | $ | id | ) |
Cascades model deletes to HABTM join keys.
- Parameters:
-
string $id ID of record that was deleted protected
Definition at line 1538 of file model.php.
References $data, $id, $table, and ConnectionManager::getDataSource().
Referenced by del(), and deleteAll().
| Model::afterDelete | ( | ) |
| Model::afterFind | ( | $ | results, | |
| $ | primary = false | |||
| ) |
After find callback. Can be used to modify any results returned by find and findAll.
- Parameters:
-
mixed $results The results of the find operation boolean $primary Whether this model is being queried directly (vs. being queried as an association)
- Returns:
- mixed Result of the find operation public
| Model::afterSave | ( | $ | created | ) |
| Model::beforeDelete | ( | $ | cascade = true |
) |
| Model::beforeFind | ( | $ | queryData | ) |
Before find callback
- Parameters:
-
array $queryData Data used to execute this query, i.e. conditions, order, etc.
- Returns:
- mixed true if the operation should continue, false if it should abort; or, modified $queryData to continue with new $queryData public
Definition at line 2465 of file model.php.
Referenced by find().
| Model::beforeSave | ( | ) |
| Model::beforeValidate | ( | ) |
Before validate callback
- Returns:
- boolean True if validate operation should continue, false to abort public
Definition at line 2519 of file model.php.
Referenced by invalidFields().
| Model::bind | ( | $ | model, | |
| $ | options = array(), |
|||
| $ | permanent = true | |||
| ) |
Bind model associations on the fly.
If $permanent is true, association will not be reset to the originals defined in the model.
- Parameters:
-
mixed $model A model or association name (string) or set of binding options (indexed by model name type) array $options If $model is a string, this is the list of association properties with which $model will be bound boolean $permanent Set to true to make the binding permanent public
Definition at line 505 of file model.php.
References $name.
| Model::bindModel | ( | $ | params, | |
| $ | reset = true | |||
| ) |
Bind model associations on the fly.
If $reset is false, association will not be reset to the originals defined in the model
Example: Add a new hasOne binding to the Profile model not defined in the model source code: $this->User->bindModel( array('hasOne' => array('Profile')) );
- Parameters:
-
array $params Set of bindings (indexed by binding type) boolean $reset Set to false to make the binding permanent
- Returns:
- boolean Success public
| Model::create | ( | $ | data = array() |
) |
Initializes the model for writing a new record, loading the default values for those fields that are not defined in $data.
- Parameters:
-
array $data Optional data to assign to the model after it is created
- Returns:
- array The current data of the model public
Definition at line 1000 of file model.php.
References $data, Set::filter(), and schema().
Referenced by invalidFields(), save(), and saveAll().
| Model::deconstruct | ( | $ | field, | |
| $ | data | |||
| ) |
Deconstructs a complex data type (array or object) into a single field value
- Parameters:
-
string $field The name of the field to be deconstructed mixed $data An array or object to be deconstructed into a field
- Returns:
- mixed The resulting data that should be assigned to a field public
Definition at line 855 of file model.php.
References $data, am(), getColumnType(), and ConnectionManager::getDataSource().
Referenced by set().
| Model::del | ( | $ | id = null, |
|
| $ | cascade = true | |||
| ) |
Removes record for given id. If no id is given, the current id is used. Returns true on success.
- Parameters:
-
mixed $id ID of record to delete boolean $cascade Set to true to delete records that depend on this record
- Returns:
- boolean True on success public
Definition at line 1440 of file model.php.
References $behaviors, $id, _clearCache(), _deleteDependent(), _deleteLinks(), afterDelete(), beforeDelete(), exists(), find(), ConnectionManager::getDataSource(), and updateCounterCache().
| Model::delete | ( | $ | id = null, |
|
| $ | cascade = true | |||
| ) |
| Model::deleteAll | ( | $ | conditions, | |
| $ | cascade = true, |
|||
| $ | callbacks = false | |||
| ) |
Allows model records to be deleted based on a set of conditions
- Parameters:
-
mixed $conditions Conditions to match boolean $cascade Set to true to delete records that depend on this record boolean $callbacks Run callbacks (not being used)
- Returns:
- boolean True on success, false on failure public
Definition at line 1566 of file model.php.
References $id, _deleteDependent(), _deleteLinks(), Set::extract(), find(), and ConnectionManager::getDataSource().
| Model::escapeField | ( | $ | field = null, |
|
| $ | alias = null | |||
| ) |
Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules.
- Parameters:
-
string $field Field to escape (e.g: id) string $alias Alias for the model (e.g: Post)
Definition at line 2230 of file model.php.
References $alias, and ConnectionManager::getDataSource().
Referenced by find(), and updateCounterCache().
| Model::execute | ( | $ | data | ) |
Runs a direct query against the bound DataSource, and returns the result.
- Parameters:
-
string $data Query data
- Returns:
- array Result of the query public
Definition at line 1858 of file model.php.
References $data, and ConnectionManager::getDataSource().
| Model::exists | ( | $ | reset = false |
) |
Returns true if a record with set id exists.
- Parameters:
-
boolean $reset if true will force database query
- Returns:
- boolean True if such a record exists public
Definition at line 1624 of file model.php.
References findCount(), and getID().
Referenced by del(), invalidFields(), and save().
| Model::field | ( | $ | name, | |
| $ | conditions = null, |
|||
| $ | order = null | |||
| ) |
Returns contents of a field in a query matching given conditions.
- Parameters:
-
string $name Name of field to get array $conditions SQL conditions (defaults to NULL) string $order SQL ORDER BY fragment
- Returns:
- string field contents, or false if not found public
Definition at line 1056 of file model.php.
References $data, $name, $order, $recursive, and find().
Referenced by getColumnType(), getColumnTypes(), and invalidFields().
| Model::find | ( | $ | conditions = null, |
|
| $ | fields = array(), |
|||
| $ | order = null, |
|||
| $ | recursive = null | |||
| ) |
Return a single row as a resultset array. By using the $recursive parameter, the call can access further "levels of association" than the ones this model is directly associated to.
Eg: find(array('name' => 'mariano.iglesias'), array('name', 'email'), 'field3 DESC', 2);
Also used to perform new-notation finds, where the first argument is type of find operation to perform (all / first / count), second parameter options for finding (indexed array, including: 'conditions', 'limit', 'recursive', 'page', 'fields', 'offset', 'order')
Eg: find('all', array( 'conditions' => array('name' => 'mariano.iglesias'), 'fields' => array('name', 'email'), 'order' => 'field3 DESC', 'recursive' => 2));
- Parameters:
-
array $conditions SQL conditions array, or type of find operation (all / first / count) mixed $fields Either a single string of a field name, or an array of field names, or options for matching string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") integer $recursive The number of levels deep to fetch associated records
- Returns:
- array Array of records public
Definition at line 1667 of file model.php.
References $behaviors, beforeFind(), Set::combine(), escapeField(), ConnectionManager::getDataSource(), and getID().
Referenced by del(), deleteAll(), field(), findAll(), findCount(), generateList(), hasAny(), isUnique(), read(), and updateCounterCache().
| Model::findAll | ( | $ | conditions = null, |
|
| $ | fields = null, |
|||
| $ | order = null, |
|||
| $ | limit = null, |
|||
| $ | page = 1, |
|||
| $ | recursive = null | |||
| ) |
Returns a resultset array with specified fields from database matching given conditions. By using the $recursive parameter, the call can access further "levels of association" than the ones this model is directly associated to.
- Parameters:
-
mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) mixed $fields Either a single string of a field name, or an array of field names string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") integer $limit SQL LIMIT clause, for calculating items per page. integer $page Page number, for accessing paged data integer $recursive The number of levels deep to fetch associated records
- Returns:
- array Array of records public
- See also:
- Model::find()
Definition at line 1798 of file model.php.
References find().
Referenced by findAllThreaded(), findNeighbours(), and generateList().
| Model::findAllThreaded | ( | $ | conditions = null, |
|
| $ | fields = null, |
|||
| $ | sort = null | |||
| ) |
Special findAll variation for tables joined to themselves. The table needs the fields id and parent_id to work.
- Parameters:
-
array $conditions Conditions for the findAll() call array $fields Fields for the findAll() call string $sort SQL ORDER BY statement
- Returns:
- array Threaded results public
Definition at line 1937 of file model.php.
References findAll().
| Model::findCount | ( | $ | conditions = null, |
|
| $ | recursive = 0 | |||
| ) |
Returns number of rows matching given SQL condition.
- Parameters:
-
array $conditions SQL conditions array for findAll integer $recursive The number of levels deep to fetch associated records
- Returns:
- integer Number of matching rows public
- See also:
- Model::find()
Definition at line 1884 of file model.php.
References find().
Referenced by exists().
| Model::findNeighbours | ( | $ | conditions = null, |
|
| $ | field, | |||
| $ | value | |||
| ) |
Returns an array with keys "prev" and "next" that holds the id's of neighbouring data, which is useful when creating paged lists.
- Parameters:
-
string $conditions SQL conditions for matching rows string $field Field name (parameter for findAll) integer $value Value from where to find neighbours
- Returns:
- array Array with keys "prev" and "next" that holds the id's public
Definition at line 1980 of file model.php.
References findAll().
| Model::generateList | ( | $ | conditions = null, |
|
| $ | order = null, |
|||
| $ | limit = null, |
|||
| $ | keyPath = null, |
|||
| $ | valuePath = null, |
|||
| $ | groupPath = null | |||
| ) |
Returns a resultset array with specified fields from database matching given conditions. Method can be used to generate option lists for SELECT elements.
- Parameters:
-
mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") integer $limit SQL LIMIT clause, for calculating items per page string $keyPath A string path to the key, i.e. "{n}.Post.id" string $valuePath A string path to the value, i.e. "{n}.Post.title" string $groupPath A string path to a value to group the elements by, i.e. "{n}.Post.category_id"
- Returns:
- array An associative array of records, where the id is the key, and the display field is the value public
Definition at line 2190 of file model.php.
References $order, $recursive, Set::combine(), find(), findAll(), and hasField().
| Model::getAffectedRows | ( | ) |
Returns the number of rows affected by the last query
- Returns:
- int Number of rows public
Definition at line 2366 of file model.php.
References ConnectionManager::getDataSource().
| Model::getAssociated | ( | $ | type = null |
) |
Gets all the models with which this model is associated
- Parameters:
-
string $type Only result associations of this type
- Returns:
- array Associations public
Definition at line 2409 of file model.php.
Referenced by normalizeFindParams(), and saveAll().
| Model::getColumnType | ( | $ | column | ) |
| Model::getColumnTypes | ( | ) |
| & Model::getDataSource | ( | ) |
Gets the DataSource to which this model is bound. Not safe for use with some versions of PHP4, because this class is overloaded.
- Returns:
- object A DataSource object public
Definition at line 2398 of file model.php.
References ConnectionManager::getDataSource().
| Model::getDisplayField | ( | ) |
| Model::getID | ( | $ | list = 0 |
) |
| Model::getInsertID | ( | ) |
Returns the ID of the last record this Model inserted
- Returns:
- mixed Last inserted ID public
Definition at line 2338 of file model.php.
Referenced by getLastInsertID().
| Model::getLastInsertID | ( | ) |
Returns the ID of the last record this Model inserted
- Returns:
- mixed Last inserted ID public
Definition at line 2329 of file model.php.
References getInsertID().
| Model::getNumRows | ( | ) |
Returns the number of rows returned from the last query
- Returns:
- int Number of rows public
Definition at line 2356 of file model.php.
References ConnectionManager::getDataSource().
| Model::hasAny | ( | $ | conditions = null |
) |
| Model::hasField | ( | $ | name | ) |
| Model::invalidate | ( | $ | field, | |
| $ | value = null | |||
| ) |
Sets a field as invalid, optionally setting the name of validation rule (in case of multiple validation for field) that was broken
- Parameters:
-
string $field The name of the field to invalidate string $value Name of validation rule that was not met public
Definition at line 2143 of file model.php.
Referenced by invalidFields().
| Model::invalidFields | ( | $ | data = array() |
) |
Returns an array of fields that do not meet validation.
- Parameters:
-
array $data Parameter usage is deprecated, set Model::$data instead
- Returns:
- array Array of invalid fields public
Definition at line 2033 of file model.php.
References $behaviors, $data, beforeValidate(), create(), exists(), field(), Validation::getInstance(), ife(), and invalidate().
Referenced by validates().
| Model::isForeignKey | ( | $ | field | ) |
| Model::isUnique | ( | $ | fields, | |
| $ | or = true | |||
| ) |
False if any fields passed match any (by default, all if $or = false) of their matching values.
- Parameters:
-
array $fields Field/value pairs to search (if no values specified, they are pulled from $this->data) boolean $or If false, all fields specified must match in order for a false return value
- Returns:
- boolean False if any records matching any fields are found public
Definition at line 1895 of file model.php.
References find().
| Model::joinModel | ( | $ | assoc, | |
| $ | keys = array() | |||
| ) |
Gets the name and fields to be used by a join model. This allows specifying join fields in the association definition.
- Parameters:
-
object $model The model to be joined mixed $with The 'with' key of the model association array $keys Any join keys which must be merged with the keys queried
- Returns:
- array
| Model::loadInfo | ( | $ | clear = false |
) |
See Model::schema
- Deprecated:
- See also:
- Model::schema()
Definition at line 921 of file model.php.
References schema().
| Model::normalizeFindParams | ( | $ | type, | |
| $ | data, | |||
| $ | altType = null, |
|||
| $ | r = array(), |
|||
| $ | _this = null | |||
| ) |
| Model::onError | ( | ) |
| Model::query | ( | ) |
Returns a resultset for given SQL statement. Generic SQL queries should be made with this method.
- Parameters:
-
string $sql SQL statement
- Returns:
- array Resultset public
Definition at line 2004 of file model.php.
References ConnectionManager::getDataSource().
| Model::read | ( | $ | fields = null, |
|
| $ | id = null | |||
| ) |
Returns a list of fields from the database, and sets the current model data (Model::$data) with the record found.
- Parameters:
-
mixed $fields String of single fieldname, or an array of fieldnames. mixed $id The ID of the record to read
- Returns:
- array Array of database fields, or false if not found public
| Model::remove | ( | $ | id = null, |
|
| $ | cascade = true | |||
| ) |
| Model::save | ( | $ | data = null, |
|
| $ | validate = true, |
|||
| $ | fieldList = array() | |||
| ) |
Saves model data to the database. By default, validation occurs before save.
- Parameters:
-
array $data Data to save. boolean $validate If set, validation will be done before the save array $fieldList List of fields to allow to be written
- Returns:
- mixed On success Model::$data if its not empty or true, false on failure public
Definition at line 1106 of file model.php.
References $behaviors, $data, $validate, _clearCache(), afterSave(), beforeSave(), create(), exists(), getColumnType(), ConnectionManager::getDataSource(), hasField(), Set::pushDiff(), updateCounterCache(), String::uuid(), and validates().
Referenced by saveAll(), and saveField().
| Model::saveAll | ( | $ | data = null, |
|
| $ | options = array() | |||
| ) |
Saves (a) multiple individual records for a single model or (b) this record, as well as all associated records
- Parameters:
-
array $data Record data to save array $options
- Returns:
- mixed True on success, or an array of validation errors on failure public
Definition at line 1349 of file model.php.
References $alias, $data, create(), getAssociated(), ConnectionManager::getDataSource(), Set::numeric(), and save().
| Model::saveField | ( | $ | name, | |
| $ | value, | |||
| $ | validate = false | |||
| ) |
Saves a single field to the database.
- Parameters:
-
string $name Name of the table field mixed $value Value of the field boolean $validate Whether or not this model should validate before saving (defaults to false)
- Returns:
- boolean True on success save public
- See also:
- Model::save()
| Model::schema | ( | $ | clear = false |
) |
Returns an array of table metadata (column names and types) from the database. $field => keys(type, null, default, key, length, extra)
- Parameters:
-
boolean $clear Set to true to reload schema
- Returns:
- array Array of table metadata public
Definition at line 903 of file model.php.
References ConnectionManager::getDataSource().
Referenced by create(), getColumnType(), getColumnTypes(), hasField(), loadInfo(), and setSource().
| Model::set | ( | $ | one, | |
| $ | two = null | |||
| ) |
This function does two things: 1) it scans the array $one for the primary key, and if that's found, it sets the current id to the value of $one[id]. For all other keys than 'id' the keys and values of $one are copied to the 'data' property of this object. 2) Returns an array with all of $one's keys and values. (Alternative indata: two strings, which are mangled to a one-item, two-dimensional array using $one for a key and $two as its value.)
- Parameters:
-
mixed $one Array or string of data string $two Value string for the alternative indata method
- Returns:
- array Data with all of $one's keys and values public
Definition at line 807 of file model.php.
References $data, Set::countDim(), deconstruct(), and Set::reverse().
Referenced by validates().
| Model::setDataSource | ( | $ | dataSource = null |
) |
Sets the DataSource to which this model is bound
- Parameters:
-
string $dataSource The name of the DataSource, as defined in Connections.php
- Returns:
- boolean True on success public
Definition at line 2377 of file model.php.
References Object::cakeError(), config(), and ConnectionManager::getData