Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 45 | class Model extends Object implements CakeEventListener { | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * The name of the DataSource connection that this Model uses  | 
            ||
| 49 | *  | 
            ||
| 50 | * The value must be an attribute name that you defined in `app/Config/database.php`  | 
            ||
| 51 | * or created using `ConnectionManager::create()`.  | 
            ||
| 52 | *  | 
            ||
| 53 | * @var string  | 
            ||
| 54 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#usedbconfig  | 
            ||
| 55 | */  | 
            ||
| 56 | public $useDbConfig = 'default';  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Custom database table name, or null/false if no table association is desired.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @var string  | 
            ||
| 62 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable  | 
            ||
| 63 | */  | 
            ||
| 64 | public $useTable = null;  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * Custom display field name. Display fields are used by Scaffold, in SELECT boxes' OPTION elements.  | 
            ||
| 68 | *  | 
            ||
| 69 |  * This field is also used in `find('list')` when called with no extra parameters in the fields list | 
            ||
| 70 | *  | 
            ||
| 71 | * @var string  | 
            ||
| 72 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#displayfield  | 
            ||
| 73 | */  | 
            ||
| 74 | public $displayField = null;  | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * Value of the primary key ID of the record that this model is currently pointing to.  | 
            ||
| 78 | * Automatically set after database insertions.  | 
            ||
| 79 | *  | 
            ||
| 80 | * @var mixed  | 
            ||
| 81 | */  | 
            ||
| 82 | public $id = false;  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Container for the data that this model gets from persistent storage (usually, a database).  | 
            ||
| 86 | *  | 
            ||
| 87 | * @var array  | 
            ||
| 88 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#data  | 
            ||
| 89 | */  | 
            ||
| 90 | public $data = array();  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * Holds physical schema/database name for this model. Automatically set during Model creation.  | 
            ||
| 94 | *  | 
            ||
| 95 | * @var string  | 
            ||
| 96 | */  | 
            ||
| 97 | public $schemaName = null;  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Table name for this Model.  | 
            ||
| 101 | *  | 
            ||
| 102 | * @var string  | 
            ||
| 103 | */  | 
            ||
| 104 | public $table = false;  | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * The name of the primary key field for this model.  | 
            ||
| 108 | *  | 
            ||
| 109 | * @var string  | 
            ||
| 110 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#primaryKey  | 
            ||
| 111 | */  | 
            ||
| 112 | public $primaryKey = null;  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * Field-by-field table metadata.  | 
            ||
| 116 | *  | 
            ||
| 117 | * @var array  | 
            ||
| 118 | */  | 
            ||
| 119 | protected $_schema = null;  | 
            ||
| 120 | |||
| 121 | /**  | 
            ||
| 122 | * List of validation rules. It must be an array with the field name as key and using  | 
            ||
| 123 | * as value one of the following possibilities  | 
            ||
| 124 | *  | 
            ||
| 125 | * ### Validating using regular expressions  | 
            ||
| 126 | *  | 
            ||
| 127 |  * {{{ | 
            ||
| 128 | * public $validate = array(  | 
            ||
| 129 | * 'name' => '/^[a-z].+$/i'  | 
            ||
| 130 | * );  | 
            ||
| 131 | * }}}  | 
            ||
| 132 | *  | 
            ||
| 133 | * ### Validating using methods (no parameters)  | 
            ||
| 134 | *  | 
            ||
| 135 |  * {{{ | 
            ||
| 136 | * public $validate = array(  | 
            ||
| 137 | * 'name' => 'notEmpty'  | 
            ||
| 138 | * );  | 
            ||
| 139 | * }}}  | 
            ||
| 140 | *  | 
            ||
| 141 | * ### Validating using methods (with parameters)  | 
            ||
| 142 | *  | 
            ||
| 143 |  * {{{ | 
            ||
| 144 | * public $validate = array(  | 
            ||
| 145 | * 'age' => array(  | 
            ||
| 146 |  *         'rule' => array('between', 5, 25) | 
            ||
| 147 | * )  | 
            ||
| 148 | * );  | 
            ||
| 149 | * }}}  | 
            ||
| 150 | *  | 
            ||
| 151 | * ### Validating using custom method  | 
            ||
| 152 | *  | 
            ||
| 153 |  * {{{ | 
            ||
| 154 | * public $validate = array(  | 
            ||
| 155 | * 'password' => array(  | 
            ||
| 156 |  *         'rule' => array('customValidation') | 
            ||
| 157 | * )  | 
            ||
| 158 | * );  | 
            ||
| 159 |  * public function customValidation($data) { | 
            ||
| 160 |  *     // $data will contain array('password' => 'value') | 
            ||
| 161 |  *     if (isset($this->data[$this->alias]['password2'])) { | 
            ||
| 162 | * return $this->data[$this->alias]['password2'] === current($data);  | 
            ||
| 163 | * }  | 
            ||
| 164 | * return true;  | 
            ||
| 165 | * }  | 
            ||
| 166 | * }}}  | 
            ||
| 167 | *  | 
            ||
| 168 | * ### Validations with messages  | 
            ||
| 169 | *  | 
            ||
| 170 | * The messages will be used in Model::$validationErrors and can be used in the FormHelper  | 
            ||
| 171 | *  | 
            ||
| 172 |  * {{{ | 
            ||
| 173 | * public $validate = array(  | 
            ||
| 174 | * 'age' => array(  | 
            ||
| 175 |  *         'rule' => array('between', 5, 25), | 
            ||
| 176 |  *         'message' => array('The age must be between %d and %d.') | 
            ||
| 177 | * )  | 
            ||
| 178 | * );  | 
            ||
| 179 | * }}}  | 
            ||
| 180 | *  | 
            ||
| 181 | * ### Multiple validations to the same field  | 
            ||
| 182 | *  | 
            ||
| 183 |  * {{{ | 
            ||
| 184 | * public $validate = array(  | 
            ||
| 185 | * 'login' => array(  | 
            ||
| 186 | * array(  | 
            ||
| 187 | * 'rule' => 'alphaNumeric',  | 
            ||
| 188 | * 'message' => 'Only alphabets and numbers allowed',  | 
            ||
| 189 | * 'last' => true  | 
            ||
| 190 | * ),  | 
            ||
| 191 | * array(  | 
            ||
| 192 |  *             'rule' => array('minLength', 8), | 
            ||
| 193 |  *             'message' => array('Minimum length of %d characters') | 
            ||
| 194 | * )  | 
            ||
| 195 | * )  | 
            ||
| 196 | * );  | 
            ||
| 197 | * }}}  | 
            ||
| 198 | *  | 
            ||
| 199 | * ### Valid keys in validations  | 
            ||
| 200 | *  | 
            ||
| 201 | * - `rule`: String with method name, regular expression (started by slash) or array with method and parameters  | 
            ||
| 202 | * - `message`: String with the message or array if have multiple parameters. See http://php.net/sprintf  | 
            ||
| 203 | * - `last`: Boolean value to indicate if continue validating the others rules if the current fail [Default: true]  | 
            ||
| 204 | * - `required`: Boolean value to indicate if the field must be present on save  | 
            ||
| 205 | * - `allowEmpty`: Boolean value to indicate if the field can be empty  | 
            ||
| 206 | * - `on`: Possible values: `update`, `create`. Indicate to apply this rule only on update or create  | 
            ||
| 207 | *  | 
            ||
| 208 | * @var array  | 
            ||
| 209 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#validate  | 
            ||
| 210 | * @link http://book.cakephp.org/2.0/en/models/data-validation.html  | 
            ||
| 211 | */  | 
            ||
| 212 | public $validate = array();  | 
            ||
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * List of validation errors.  | 
            ||
| 216 | *  | 
            ||
| 217 | * @var array  | 
            ||
| 218 | */  | 
            ||
| 219 | public $validationErrors = array();  | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Name of the validation string domain to use when translating validation errors.  | 
            ||
| 223 | *  | 
            ||
| 224 | * @var string  | 
            ||
| 225 | */  | 
            ||
| 226 | public $validationDomain = null;  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * Database table prefix for tables in model.  | 
            ||
| 230 | *  | 
            ||
| 231 | * @var string  | 
            ||
| 232 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#tableprefix  | 
            ||
| 233 | */  | 
            ||
| 234 | public $tablePrefix = null;  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * Plugin model belongs to.  | 
            ||
| 238 | *  | 
            ||
| 239 | * @var string  | 
            ||
| 240 | */  | 
            ||
| 241 | public $plugin = null;  | 
            ||
| 242 | |||
| 243 | /**  | 
            ||
| 244 | * Name of the model.  | 
            ||
| 245 | *  | 
            ||
| 246 | * @var string  | 
            ||
| 247 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#name  | 
            ||
| 248 | */  | 
            ||
| 249 | public $name = null;  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Alias name for model.  | 
            ||
| 253 | *  | 
            ||
| 254 | * @var string  | 
            ||
| 255 | */  | 
            ||
| 256 | public $alias = null;  | 
            ||
| 257 | |||
| 258 | /**  | 
            ||
| 259 | * List of table names included in the model description. Used for associations.  | 
            ||
| 260 | *  | 
            ||
| 261 | * @var array  | 
            ||
| 262 | */  | 
            ||
| 263 | public $tableToModel = array();  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * Whether or not to cache queries for this model. This enables in-memory  | 
            ||
| 267 | * caching only, the results are not stored beyond the current request.  | 
            ||
| 268 | *  | 
            ||
| 269 | * @var boolean  | 
            ||
| 270 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#cachequeries  | 
            ||
| 271 | */  | 
            ||
| 272 | public $cacheQueries = false;  | 
            ||
| 273 | |||
| 274 | /**  | 
            ||
| 275 | * Detailed list of belongsTo associations.  | 
            ||
| 276 | *  | 
            ||
| 277 | * ### Basic usage  | 
            ||
| 278 | *  | 
            ||
| 279 |  * `public $belongsTo = array('Group', 'Department');` | 
            ||
| 280 | *  | 
            ||
| 281 | * ### Detailed configuration  | 
            ||
| 282 | *  | 
            ||
| 283 |  * {{{ | 
            ||
| 284 | * public $belongsTo = array(  | 
            ||
| 285 | * 'Group',  | 
            ||
| 286 | * 'Department' => array(  | 
            ||
| 287 | * 'className' => 'Department',  | 
            ||
| 288 | * 'foreignKey' => 'department_id'  | 
            ||
| 289 | * )  | 
            ||
| 290 | * );  | 
            ||
| 291 | * }}}  | 
            ||
| 292 | *  | 
            ||
| 293 | * ### Possible keys in association  | 
            ||
| 294 | *  | 
            ||
| 295 | * - `className`: the class name of the model being associated to the current model.  | 
            ||
| 296 | * If you're defining a 'Profile belongsTo User' relationship, the className key should equal 'User.'  | 
            ||
| 297 | * - `foreignKey`: the name of the foreign key found in the current model. This is  | 
            ||
| 298 | * especially handy if you need to define multiple belongsTo relationships. The default  | 
            ||
| 299 | * value for this key is the underscored, singular name of the other model, suffixed with '_id'.  | 
            ||
| 300 | * - `conditions`: An SQL fragment used to filter related model records. It's good  | 
            ||
| 301 | * practice to use model names in SQL fragments: 'User.active = 1' is always  | 
            ||
| 302 | * better than just 'active = 1.'  | 
            ||
| 303 | * - `type`: the type of the join to use in the SQL query, default is LEFT which  | 
            ||
| 304 | * may not fit your needs in all situations, INNER may be helpful when you want  | 
            ||
| 305 | * everything from your main and associated models or nothing at all!(effective  | 
            ||
| 306 | * when used with some conditions of course). (NB: type value is in lower case - i.e. left, inner)  | 
            ||
| 307 | * - `fields`: A list of fields to be retrieved when the associated model data is  | 
            ||
| 308 | * fetched. Returns all fields by default.  | 
            ||
| 309 | * - `order`: An SQL fragment that defines the sorting order for the returned associated rows.  | 
            ||
| 310 | * - `counterCache`: If set to true the associated Model will automatically increase or  | 
            ||
| 311 | * decrease the "[singular_model_name]_count" field in the foreign table whenever you do  | 
            ||
| 312 | * a save() or delete(). If its a string then its the field name to use. The value in the  | 
            ||
| 313 | * counter field represents the number of related rows.  | 
            ||
| 314 | * - `counterScope`: Optional conditions array to use for updating counter cache field.  | 
            ||
| 315 | *  | 
            ||
| 316 | * @var array  | 
            ||
| 317 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto  | 
            ||
| 318 | */  | 
            ||
| 319 | public $belongsTo = array();  | 
            ||
| 320 | |||
| 321 | /**  | 
            ||
| 322 | * Detailed list of hasOne associations.  | 
            ||
| 323 | *  | 
            ||
| 324 | * ### Basic usage  | 
            ||
| 325 | *  | 
            ||
| 326 |  * `public $hasOne = array('Profile', 'Address');` | 
            ||
| 327 | *  | 
            ||
| 328 | * ### Detailed configuration  | 
            ||
| 329 | *  | 
            ||
| 330 |  * {{{ | 
            ||
| 331 | * public $hasOne = array(  | 
            ||
| 332 | * 'Profile',  | 
            ||
| 333 | * 'Address' => array(  | 
            ||
| 334 | * 'className' => 'Address',  | 
            ||
| 335 | * 'foreignKey' => 'user_id'  | 
            ||
| 336 | * )  | 
            ||
| 337 | * );  | 
            ||
| 338 | * }}}  | 
            ||
| 339 | *  | 
            ||
| 340 | * ### Possible keys in association  | 
            ||
| 341 | *  | 
            ||
| 342 | * - `className`: the class name of the model being associated to the current model.  | 
            ||
| 343 | * If you're defining a 'User hasOne Profile' relationship, the className key should equal 'Profile.'  | 
            ||
| 344 | * - `foreignKey`: the name of the foreign key found in the other model. This is  | 
            ||
| 345 | * especially handy if you need to define multiple hasOne relationships.  | 
            ||
| 346 | * The default value for this key is the underscored, singular name of the  | 
            ||
| 347 | * current model, suffixed with '_id'. In the example above it would default to 'user_id'.  | 
            ||
| 348 | * - `conditions`: An SQL fragment used to filter related model records. It's good  | 
            ||
| 349 | * practice to use model names in SQL fragments: "Profile.approved = 1" is  | 
            ||
| 350 | * always better than just "approved = 1."  | 
            ||
| 351 | * - `fields`: A list of fields to be retrieved when the associated model data is  | 
            ||
| 352 | * fetched. Returns all fields by default.  | 
            ||
| 353 | * - `order`: An SQL fragment that defines the sorting order for the returned associated rows.  | 
            ||
| 354 | * - `dependent`: When the dependent key is set to true, and the model's delete()  | 
            ||
| 355 | * method is called with the cascade parameter set to true, associated model  | 
            ||
| 356 | * records are also deleted. In this case we set it true so that deleting a  | 
            ||
| 357 | * User will also delete her associated Profile.  | 
            ||
| 358 | *  | 
            ||
| 359 | * @var array  | 
            ||
| 360 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone  | 
            ||
| 361 | */  | 
            ||
| 362 | public $hasOne = array();  | 
            ||
| 363 | |||
| 364 | /**  | 
            ||
| 365 | * Detailed list of hasMany associations.  | 
            ||
| 366 | *  | 
            ||
| 367 | * ### Basic usage  | 
            ||
| 368 | *  | 
            ||
| 369 |  * `public $hasMany = array('Comment', 'Task');` | 
            ||
| 370 | *  | 
            ||
| 371 | * ### Detailed configuration  | 
            ||
| 372 | *  | 
            ||
| 373 |  * {{{ | 
            ||
| 374 | * public $hasMany = array(  | 
            ||
| 375 | * 'Comment',  | 
            ||
| 376 | * 'Task' => array(  | 
            ||
| 377 | * 'className' => 'Task',  | 
            ||
| 378 | * 'foreignKey' => 'user_id'  | 
            ||
| 379 | * )  | 
            ||
| 380 | * );  | 
            ||
| 381 | * }}}  | 
            ||
| 382 | *  | 
            ||
| 383 | * ### Possible keys in association  | 
            ||
| 384 | *  | 
            ||
| 385 | * - `className`: the class name of the model being associated to the current model.  | 
            ||
| 386 | * If you're defining a 'User hasMany Comment' relationship, the className key should equal 'Comment.'  | 
            ||
| 387 | * - `foreignKey`: the name of the foreign key found in the other model. This is  | 
            ||
| 388 | * especially handy if you need to define multiple hasMany relationships. The default  | 
            ||
| 389 | * value for this key is the underscored, singular name of the actual model, suffixed with '_id'.  | 
            ||
| 390 | * - `conditions`: An SQL fragment used to filter related model records. It's good  | 
            ||
| 391 | * practice to use model names in SQL fragments: "Comment.status = 1" is always  | 
            ||
| 392 | * better than just "status = 1."  | 
            ||
| 393 | * - `fields`: A list of fields to be retrieved when the associated model data is  | 
            ||
| 394 | * fetched. Returns all fields by default.  | 
            ||
| 395 | * - `order`: An SQL fragment that defines the sorting order for the returned associated rows.  | 
            ||
| 396 | * - `limit`: The maximum number of associated rows you want returned.  | 
            ||
| 397 | * - `offset`: The number of associated rows to skip over (given the current  | 
            ||
| 398 | * conditions and order) before fetching and associating.  | 
            ||
| 399 | * - `dependent`: When dependent is set to true, recursive model deletion is  | 
            ||
| 400 | * possible. In this example, Comment records will be deleted when their  | 
            ||
| 401 | * associated User record has been deleted.  | 
            ||
| 402 | * - `exclusive`: When exclusive is set to true, recursive model deletion does  | 
            ||
| 403 | * the delete with a deleteAll() call, instead of deleting each entity separately.  | 
            ||
| 404 | * This greatly improves performance, but may not be ideal for all circumstances.  | 
            ||
| 405 | * - `finderQuery`: A complete SQL query CakePHP can use to fetch associated model  | 
            ||
| 406 | * records. This should be used in situations that require very custom results.  | 
            ||
| 407 | *  | 
            ||
| 408 | * @var array  | 
            ||
| 409 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany  | 
            ||
| 410 | */  | 
            ||
| 411 | public $hasMany = array();  | 
            ||
| 412 | |||
| 413 | /**  | 
            ||
| 414 | * Detailed list of hasAndBelongsToMany associations.  | 
            ||
| 415 | *  | 
            ||
| 416 | * ### Basic usage  | 
            ||
| 417 | *  | 
            ||
| 418 |  * `public $hasAndBelongsToMany = array('Role', 'Address');` | 
            ||
| 419 | *  | 
            ||
| 420 | * ### Detailed configuration  | 
            ||
| 421 | *  | 
            ||
| 422 |  * {{{ | 
            ||
| 423 | * public $hasAndBelongsToMany = array(  | 
            ||
| 424 | * 'Role',  | 
            ||
| 425 | * 'Address' => array(  | 
            ||
| 426 | * 'className' => 'Address',  | 
            ||
| 427 | * 'foreignKey' => 'user_id',  | 
            ||
| 428 | * 'associationForeignKey' => 'address_id',  | 
            ||
| 429 | * 'joinTable' => 'addresses_users'  | 
            ||
| 430 | * )  | 
            ||
| 431 | * );  | 
            ||
| 432 | * }}}  | 
            ||
| 433 | *  | 
            ||
| 434 | * ### Possible keys in association  | 
            ||
| 435 | *  | 
            ||
| 436 | * - `className`: the class name of the model being associated to the current model.  | 
            ||
| 437 | * If you're defining a 'Recipe HABTM Tag' relationship, the className key should equal 'Tag.'  | 
            ||
| 438 | * - `joinTable`: The name of the join table used in this association (if the  | 
            ||
| 439 | * current table doesn't adhere to the naming convention for HABTM join tables).  | 
            ||
| 440 | * - `with`: Defines the name of the model for the join table. By default CakePHP  | 
            ||
| 441 | * will auto-create a model for you. Using the example above it would be called  | 
            ||
| 442 | * RecipesTag. By using this key you can override this default name. The join  | 
            ||
| 443 | * table model can be used just like any "regular" model to access the join table directly.  | 
            ||
| 444 | * - `foreignKey`: the name of the foreign key found in the current model.  | 
            ||
| 445 | * This is especially handy if you need to define multiple HABTM relationships.  | 
            ||
| 446 | * The default value for this key is the underscored, singular name of the  | 
            ||
| 447 | * current model, suffixed with '_id'.  | 
            ||
| 448 | * - `associationForeignKey`: the name of the foreign key found in the other model.  | 
            ||
| 449 | * This is especially handy if you need to define multiple HABTM relationships.  | 
            ||
| 450 | * The default value for this key is the underscored, singular name of the other  | 
            ||
| 451 | * model, suffixed with '_id'.  | 
            ||
| 452 | * - `unique`: If true (default value) cake will first delete existing relationship  | 
            ||
| 453 | * records in the foreign keys table before inserting new ones, when updating a  | 
            ||
| 454 | * record. So existing associations need to be passed again when updating.  | 
            ||
| 455 | * To prevent deletion of existing relationship records, set this key to a string 'keepExisting'.  | 
            ||
| 456 | * - `conditions`: An SQL fragment used to filter related model records. It's good  | 
            ||
| 457 | * practice to use model names in SQL fragments: "Comment.status = 1" is always  | 
            ||
| 458 | * better than just "status = 1."  | 
            ||
| 459 | * - `fields`: A list of fields to be retrieved when the associated model data is  | 
            ||
| 460 | * fetched. Returns all fields by default.  | 
            ||
| 461 | * - `order`: An SQL fragment that defines the sorting order for the returned associated rows.  | 
            ||
| 462 | * - `limit`: The maximum number of associated rows you want returned.  | 
            ||
| 463 | * - `offset`: The number of associated rows to skip over (given the current  | 
            ||
| 464 | * conditions and order) before fetching and associating.  | 
            ||
| 465 | * - `finderQuery`, A complete SQL query CakePHP  | 
            ||
| 466 | * can use to fetch associated model records. This should  | 
            ||
| 467 | * be used in situations that require very custom results.  | 
            ||
| 468 | *  | 
            ||
| 469 | * @var array  | 
            ||
| 470 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm  | 
            ||
| 471 | */  | 
            ||
| 472 | public $hasAndBelongsToMany = array();  | 
            ||
| 473 | |||
| 474 | /**  | 
            ||
| 475 | * List of behaviors to load when the model object is initialized. Settings can be  | 
            ||
| 476 | * passed to behaviors by using the behavior name as index. Eg:  | 
            ||
| 477 | *  | 
            ||
| 478 |  * public $actsAs = array('Translate', 'MyBehavior' => array('setting1' => 'value1')) | 
            ||
| 479 | *  | 
            ||
| 480 | * @var array  | 
            ||
| 481 | * @link http://book.cakephp.org/2.0/en/models/behaviors.html#using-behaviors  | 
            ||
| 482 | */  | 
            ||
| 483 | public $actsAs = null;  | 
            ||
| 484 | |||
| 485 | /**  | 
            ||
| 486 | * Holds the Behavior objects currently bound to this model.  | 
            ||
| 487 | *  | 
            ||
| 488 | * @var BehaviorCollection  | 
            ||
| 489 | */  | 
            ||
| 490 | public $Behaviors = null;  | 
            ||
| 491 | |||
| 492 | /**  | 
            ||
| 493 | * Whitelist of fields allowed to be saved.  | 
            ||
| 494 | *  | 
            ||
| 495 | * @var array  | 
            ||
| 496 | */  | 
            ||
| 497 | public $whitelist = array();  | 
            ||
| 498 | |||
| 499 | /**  | 
            ||
| 500 | * Whether or not to cache sources for this model.  | 
            ||
| 501 | *  | 
            ||
| 502 | * @var boolean  | 
            ||
| 503 | */  | 
            ||
| 504 | public $cacheSources = true;  | 
            ||
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Type of find query currently executing.  | 
            ||
| 508 | *  | 
            ||
| 509 | * @var string  | 
            ||
| 510 | */  | 
            ||
| 511 | public $findQueryType = null;  | 
            ||
| 512 | |||
| 513 | /**  | 
            ||
| 514 | * Number of associations to recurse through during find calls. Fetches only  | 
            ||
| 515 | * the first level by default.  | 
            ||
| 516 | *  | 
            ||
| 517 | * @var integer  | 
            ||
| 518 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive  | 
            ||
| 519 | */  | 
            ||
| 520 | public $recursive = 1;  | 
            ||
| 521 | |||
| 522 | /**  | 
            ||
| 523 | * The column name(s) and direction(s) to order find results by default.  | 
            ||
| 524 | *  | 
            ||
| 525 | * public $order = "Post.created DESC";  | 
            ||
| 526 |  * public $order = array("Post.view_count DESC", "Post.rating DESC"); | 
            ||
| 527 | *  | 
            ||
| 528 | * @var string  | 
            ||
| 529 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#order  | 
            ||
| 530 | */  | 
            ||
| 531 | public $order = null;  | 
            ||
| 532 | |||
| 533 | /**  | 
            ||
| 534 | * Array of virtual fields this model has. Virtual fields are aliased  | 
            ||
| 535 | * SQL expressions. Fields added to this property will be read as other fields in a model  | 
            ||
| 536 | * but will not be saveable.  | 
            ||
| 537 | *  | 
            ||
| 538 |  * `public $virtualFields = array('two' => '1 + 1');` | 
            ||
| 539 | *  | 
            ||
| 540 | * Is a simplistic example of how to set virtualFields  | 
            ||
| 541 | *  | 
            ||
| 542 | * @var array  | 
            ||
| 543 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#virtualfields  | 
            ||
| 544 | */  | 
            ||
| 545 | public $virtualFields = array();  | 
            ||
| 546 | |||
| 547 | /**  | 
            ||
| 548 | * Default list of association keys.  | 
            ||
| 549 | *  | 
            ||
| 550 | * @var array  | 
            ||
| 551 | */  | 
            ||
| 552 | protected $_associationKeys = array(  | 
            ||
| 553 | 		'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'), | 
            ||
| 554 | 		'hasOne' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'dependent'), | 
            ||
| 555 | 		'hasMany' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'), | 
            ||
| 556 | 		'hasAndBelongsToMany' => array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery') | 
            ||
| 557 | );  | 
            ||
| 558 | |||
| 559 | /**  | 
            ||
| 560 | * Holds provided/generated association key names and other data for all associations.  | 
            ||
| 561 | *  | 
            ||
| 562 | * @var array  | 
            ||
| 563 | */  | 
            ||
| 564 | 	protected $_associations = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'); | 
            ||
| 565 | |||
| 566 | // @codingStandardsIgnoreStart  | 
            ||
| 567 | |||
| 568 | /**  | 
            ||
| 569 | * Holds model associations temporarily to allow for dynamic (un)binding.  | 
            ||
| 570 | *  | 
            ||
| 571 | * @var array  | 
            ||
| 572 | */  | 
            ||
| 573 | public $__backAssociation = array();  | 
            ||
| 574 | |||
| 575 | /**  | 
            ||
| 576 | * Back inner association  | 
            ||
| 577 | *  | 
            ||
| 578 | * @var array  | 
            ||
| 579 | */  | 
            ||
| 580 | public $__backInnerAssociation = array();  | 
            ||
| 581 | |||
| 582 | /**  | 
            ||
| 583 | * Back original association  | 
            ||
| 584 | *  | 
            ||
| 585 | * @var array  | 
            ||
| 586 | */  | 
            ||
| 587 | public $__backOriginalAssociation = array();  | 
            ||
| 588 | |||
| 589 | /**  | 
            ||
| 590 | * Back containable association  | 
            ||
| 591 | *  | 
            ||
| 592 | * @var array  | 
            ||
| 593 | */  | 
            ||
| 594 | public $__backContainableAssociation = array();  | 
            ||
| 595 | |||
| 596 | // @codingStandardsIgnoreEnd  | 
            ||
| 597 | |||
| 598 | /**  | 
            ||
| 599 | * The ID of the model record that was last inserted.  | 
            ||
| 600 | *  | 
            ||
| 601 | * @var integer  | 
            ||
| 602 | */  | 
            ||
| 603 | protected $_insertID = null;  | 
            ||
| 604 | |||
| 605 | /**  | 
            ||
| 606 | * Has the datasource been configured.  | 
            ||
| 607 | *  | 
            ||
| 608 | * @var boolean  | 
            ||
| 609 | * @see Model::getDataSource  | 
            ||
| 610 | */  | 
            ||
| 611 | protected $_sourceConfigured = false;  | 
            ||
| 612 | |||
| 613 | /**  | 
            ||
| 614 | * List of valid finder method options, supplied as the first parameter to find().  | 
            ||
| 615 | *  | 
            ||
| 616 | * @var array  | 
            ||
| 617 | */  | 
            ||
| 618 | public $findMethods = array(  | 
            ||
| 619 | 'all' => true, 'first' => true, 'count' => true,  | 
            ||
| 620 | 'neighbors' => true, 'list' => true, 'threaded' => true  | 
            ||
| 621 | );  | 
            ||
| 622 | |||
| 623 | /**  | 
            ||
| 624 | * Instance of the CakeEventManager this model is using  | 
            ||
| 625 | * to dispatch inner events.  | 
            ||
| 626 | *  | 
            ||
| 627 | * @var CakeEventManager  | 
            ||
| 628 | */  | 
            ||
| 629 | protected $_eventManager = null;  | 
            ||
| 630 | |||
| 631 | /**  | 
            ||
| 632 | * Instance of the ModelValidator  | 
            ||
| 633 | *  | 
            ||
| 634 | * @var ModelValidator  | 
            ||
| 635 | */  | 
            ||
| 636 | protected $_validator = null;  | 
            ||
| 637 | |||
| 638 | /**  | 
            ||
| 639 | * Constructor. Binds the model's database table to the object.  | 
            ||
| 640 | *  | 
            ||
| 641 | * If `$id` is an array it can be used to pass several options into the model.  | 
            ||
| 642 | *  | 
            ||
| 643 | * - `id`: The id to start the model on.  | 
            ||
| 644 | * - `table`: The table to use for this model.  | 
            ||
| 645 | * - `ds`: The connection name this model is connected to.  | 
            ||
| 646 | * - `name`: The name of the model eg. Post.  | 
            ||
| 647 | * - `alias`: The alias of the model, this is used for registering the instance in the `ClassRegistry`.  | 
            ||
| 648 | * eg. `ParentThread`  | 
            ||
| 649 | *  | 
            ||
| 650 | * ### Overriding Model's __construct method.  | 
            ||
| 651 | *  | 
            ||
| 652 | * When overriding Model::__construct() be careful to include and pass in all 3 of the  | 
            ||
| 653 | * arguments to `parent::__construct($id, $table, $ds);`  | 
            ||
| 654 | *  | 
            ||
| 655 | * ### Dynamically creating models  | 
            ||
| 656 | *  | 
            ||
| 657 | * You can dynamically create model instances using the $id array syntax.  | 
            ||
| 658 | *  | 
            ||
| 659 |  * {{{ | 
            ||
| 660 |  * $Post = new Model(array('table' => 'posts', 'name' => 'Post', 'ds' => 'connection2')); | 
            ||
| 661 | * }}}  | 
            ||
| 662 | *  | 
            ||
| 663 | * Would create a model attached to the posts table on connection2. Dynamic model creation is useful  | 
            ||
| 664 | * when you want a model object that contains no associations or attached behaviors.  | 
            ||
| 665 | *  | 
            ||
| 666 | * @param boolean|integer|string|array $id Set this ID for this model on startup,  | 
            ||
| 667 | * can also be an array of options, see above.  | 
            ||
| 668 | * @param string $table Name of database table to use.  | 
            ||
| 669 | * @param string $ds DataSource connection name.  | 
            ||
| 670 | */  | 
            ||
| 671 | 	public function __construct($id = false, $table = null, $ds = null) { | 
            ||
| 749 | |||
| 750 | /**  | 
            ||
| 751 | * Returns a list of all events that will fire in the model during it's lifecycle.  | 
            ||
| 752 | * You can override this function to add you own listener callbacks  | 
            ||
| 753 | *  | 
            ||
| 754 | * @return array  | 
            ||
| 755 | */  | 
            ||
| 756 | 	public function implementedEvents() { | 
            ||
| 768 | |||
| 769 | /**  | 
            ||
| 770 | * Returns the CakeEventManager manager instance that is handling any callbacks.  | 
            ||
| 771 | * You can use this instance to register any new listeners or callbacks to the  | 
            ||
| 772 | * model events, or create your own events and trigger them at will.  | 
            ||
| 773 | *  | 
            ||
| 774 | * @return CakeEventManager  | 
            ||
| 775 | */  | 
            ||
| 776 | View Code Duplication | 	public function getEventManager() { | 
            |
| 785 | |||
| 786 | /**  | 
            ||
| 787 | * Handles custom method calls, like findBy<field> for DB models,  | 
            ||
| 788 | * and custom RPC calls for remote data sources.  | 
            ||
| 789 | *  | 
            ||
| 790 | * @param string $method Name of method to call.  | 
            ||
| 791 | * @param array $params Parameters for the method.  | 
            ||
| 792 | * @return mixed Whatever is returned by called method  | 
            ||
| 793 | */  | 
            ||
| 794 | 	public function __call($method, $params) { | 
            ||
| 802 | |||
| 803 | /**  | 
            ||
| 804 | * Handles the lazy loading of model associations by looking in the association arrays for the requested variable  | 
            ||
| 805 | *  | 
            ||
| 806 | * @param string $name variable tested for existence in class  | 
            ||
| 807 | * @return boolean true if the variable exists (if is a not loaded model association it will be created), false otherwise  | 
            ||
| 808 | */  | 
            ||
| 809 | 	public function __isset($name) { | 
            ||
| 871 | |||
| 872 | /**  | 
            ||
| 873 | * Returns the value of the requested variable if it can be set by __isset()  | 
            ||
| 874 | *  | 
            ||
| 875 | * @param string $name variable requested for it's value or reference  | 
            ||
| 876 | * @return mixed value of requested variable if it is set  | 
            ||
| 877 | */  | 
            ||
| 878 | 	public function __get($name) { | 
            ||
| 896 | |||
| 897 | /**  | 
            ||
| 898 | * Bind model associations on the fly.  | 
            ||
| 899 | *  | 
            ||
| 900 | * If `$reset` is false, association will not be reset  | 
            ||
| 901 | * to the originals defined in the model  | 
            ||
| 902 | *  | 
            ||
| 903 | * Example: Add a new hasOne binding to the Profile model not  | 
            ||
| 904 | * defined in the model source code:  | 
            ||
| 905 | *  | 
            ||
| 906 |  * `$this->User->bindModel(array('hasOne' => array('Profile')));` | 
            ||
| 907 | *  | 
            ||
| 908 | * Bindings that are not made permanent will be reset by the next Model::find() call on this  | 
            ||
| 909 | * model.  | 
            ||
| 910 | *  | 
            ||
| 911 | * @param array $params Set of bindings (indexed by binding type)  | 
            ||
| 912 | * @param boolean $reset Set to false to make the binding permanent  | 
            ||
| 913 | * @return boolean Success  | 
            ||
| 914 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly  | 
            ||
| 915 | */  | 
            ||
| 916 | 	public function bindModel($params, $reset = true) { | 
            ||
| 945 | |||
| 946 | /**  | 
            ||
| 947 | * Turn off associations on the fly.  | 
            ||
| 948 | *  | 
            ||
| 949 | * If $reset is false, association will not be reset  | 
            ||
| 950 | * to the originals defined in the model  | 
            ||
| 951 | *  | 
            ||
| 952 | * Example: Turn off the associated Model Support request,  | 
            ||
| 953 | * to temporarily lighten the User model:  | 
            ||
| 954 | *  | 
            ||
| 955 |  * `$this->User->unbindModel(array('hasMany' => array('Supportrequest')));` | 
            ||
| 956 | *  | 
            ||
| 957 | * unbound models that are not made permanent will reset with the next call to Model::find()  | 
            ||
| 958 | *  | 
            ||
| 959 | * @param array $params Set of bindings to unbind (indexed by binding type)  | 
            ||
| 960 | * @param boolean $reset Set to false to make the unbinding permanent  | 
            ||
| 961 | * @return boolean Success  | 
            ||
| 962 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly  | 
            ||
| 963 | */  | 
            ||
| 964 | 	public function unbindModel($params, $reset = true) { | 
            ||
| 981 | |||
| 982 | /**  | 
            ||
| 983 | * Create a set of associations.  | 
            ||
| 984 | *  | 
            ||
| 985 | * @return void  | 
            ||
| 986 | */  | 
            ||
| 987 | 	protected function _createLinks() { | 
            ||
| 1023 | |||
| 1024 | /**  | 
            ||
| 1025 | * Protected helper method to create associated models of a given class.  | 
            ||
| 1026 | *  | 
            ||
| 1027 | * @param string $assoc Association name  | 
            ||
| 1028 | * @param string $className Class name  | 
            ||
| 1029 | * @param string $plugin name of the plugin where $className is located  | 
            ||
| 1030 |  * 	examples: public $hasMany = array('Assoc' => array('className' => 'ModelName')); | 
            ||
| 1031 | * usage: $this->Assoc->modelMethods();  | 
            ||
| 1032 | *  | 
            ||
| 1033 |  * 				public $hasMany = array('ModelName'); | 
            ||
| 1034 | * usage: $this->ModelName->modelMethods();  | 
            ||
| 1035 | * @return void  | 
            ||
| 1036 | */  | 
            ||
| 1037 | 	protected function _constructLinkedModel($assoc, $className = null, $plugin = null) { | 
            ||
| 1059 | |||
| 1060 | /**  | 
            ||
| 1061 | * Build an array-based association from string.  | 
            ||
| 1062 | *  | 
            ||
| 1063 | * @param string $type 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'  | 
            ||
| 1064 | * @param string $assocKey  | 
            ||
| 1065 | * @return void  | 
            ||
| 1066 | */  | 
            ||
| 1067 | 	protected function _generateAssociation($type, $assocKey) { | 
            ||
| 1117 | |||
| 1118 | /**  | 
            ||
| 1119 | * Sets a custom table for your model class. Used by your controller to select a database table.  | 
            ||
| 1120 | *  | 
            ||
| 1121 | * @param string $tableName Name of the custom table  | 
            ||
| 1122 | * @throws MissingTableException when database table $tableName is not found on data source  | 
            ||
| 1123 | * @return void  | 
            ||
| 1124 | */  | 
            ||
| 1125 | 	public function setSource($tableName) { | 
            ||
| 1151 | |||
| 1152 | /**  | 
            ||
| 1153 | * This function does two things:  | 
            ||
| 1154 | *  | 
            ||
| 1155 | * 1. it scans the array $one for the primary key,  | 
            ||
| 1156 | * and if that's found, it sets the current id to the value of $one[id].  | 
            ||
| 1157 | * For all other keys than 'id' the keys and values of $one are copied to the 'data' property of this object.  | 
            ||
| 1158 | * 2. Returns an array with all of $one's keys and values.  | 
            ||
| 1159 | * (Alternative indata: two strings, which are mangled to  | 
            ||
| 1160 | * a one-item, two-dimensional array using $one for a key and $two as its value.)  | 
            ||
| 1161 | *  | 
            ||
| 1162 | * @param string|array|SimpleXmlElement|DomNode $one Array or string of data  | 
            ||
| 1163 | * @param string $two Value string for the alternative indata method  | 
            ||
| 1164 | * @return array Data with all of $one's keys and values  | 
            ||
| 1165 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html  | 
            ||
| 1166 | */  | 
            ||
| 1167 | 	public function set($one, $two = null) { | 
            ||
| 1211 | |||
| 1212 | /**  | 
            ||
| 1213 | * Move values to alias  | 
            ||
| 1214 | *  | 
            ||
| 1215 | * @param array $data  | 
            ||
| 1216 | * @return array  | 
            ||
| 1217 | */  | 
            ||
| 1218 | 	protected function _setAliasData($data) { | 
            ||
| 1231 | |||
| 1232 | /**  | 
            ||
| 1233 | * Normalize `Xml::toArray()` to use in `Model::save()`  | 
            ||
| 1234 | *  | 
            ||
| 1235 | * @param array $xml XML as array  | 
            ||
| 1236 | * @return array  | 
            ||
| 1237 | */  | 
            ||
| 1238 | 	protected function _normalizeXmlData(array $xml) { | 
            ||
| 1252 | |||
| 1253 | /**  | 
            ||
| 1254 | * Deconstructs a complex data type (array or object) into a single field value.  | 
            ||
| 1255 | *  | 
            ||
| 1256 | * @param string $field The name of the field to be deconstructed  | 
            ||
| 1257 | * @param array|object $data An array or object to be deconstructed into a field  | 
            ||
| 1258 | * @return mixed The resulting data that should be assigned to a field  | 
            ||
| 1259 | */  | 
            ||
| 1260 | 	public function deconstruct($field, $data) { | 
            ||
| 1345 | |||
| 1346 | /**  | 
            ||
| 1347 | * Returns an array of table metadata (column names and types) from the database.  | 
            ||
| 1348 | * $field => keys(type, null, default, key, length, extra)  | 
            ||
| 1349 | *  | 
            ||
| 1350 | * @param boolean|string $field Set to true to reload schema, or a string to return a specific field  | 
            ||
| 1351 | * @return array Array of table metadata  | 
            ||
| 1352 | */  | 
            ||
| 1353 | 	public function schema($field = false) { | 
            ||
| 1372 | |||
| 1373 | /**  | 
            ||
| 1374 | * Returns an associative array of field names and column types.  | 
            ||
| 1375 | *  | 
            ||
| 1376 | * @return array Field types indexed by field name  | 
            ||
| 1377 | */  | 
            ||
| 1378 | 	public function getColumnTypes() { | 
            ||
| 1391 | |||
| 1392 | /**  | 
            ||
| 1393 | * Returns the column type of a column in the model.  | 
            ||
| 1394 | *  | 
            ||
| 1395 | * @param string $column The name of the model column  | 
            ||
| 1396 | * @return string Column type  | 
            ||
| 1397 | */  | 
            ||
| 1398 | 	public function getColumnType($column) { | 
            ||
| 1421 | |||
| 1422 | /**  | 
            ||
| 1423 | * Returns true if the supplied field exists in the model's database table.  | 
            ||
| 1424 | *  | 
            ||
| 1425 | * @param string|array $name Name of field to look for, or an array of names  | 
            ||
| 1426 | * @param boolean $checkVirtual checks if the field is declared as virtual  | 
            ||
| 1427 | * @return mixed If $name is a string, returns a boolean indicating whether the field exists.  | 
            ||
| 1428 | * If $name is an array of field names, returns the first field that exists,  | 
            ||
| 1429 | * or false if none exist.  | 
            ||
| 1430 | */  | 
            ||
| 1431 | 	public function hasField($name, $checkVirtual = false) { | 
            ||
| 1456 | |||
| 1457 | /**  | 
            ||
| 1458 | * Check that a method is callable on a model. This will check both the model's own methods, its  | 
            ||
| 1459 | * inherited methods and methods that could be callable through behaviors.  | 
            ||
| 1460 | *  | 
            ||
| 1461 | * @param string $method The method to be called.  | 
            ||
| 1462 | * @return boolean True on method being callable.  | 
            ||
| 1463 | */  | 
            ||
| 1464 | 	public function hasMethod($method) { | 
            ||
| 1471 | |||
| 1472 | /**  | 
            ||
| 1473 | * Returns true if the supplied field is a model Virtual Field  | 
            ||
| 1474 | *  | 
            ||
| 1475 | * @param string $field Name of field to look for  | 
            ||
| 1476 | * @return boolean indicating whether the field exists as a model virtual field.  | 
            ||
| 1477 | */  | 
            ||
| 1478 | 	public function isVirtualField($field) { | 
            ||
| 1496 | |||
| 1497 | /**  | 
            ||
| 1498 | * Returns the expression for a model virtual field  | 
            ||
| 1499 | *  | 
            ||
| 1500 | * @param string $field Name of field to look for  | 
            ||
| 1501 | * @return mixed If $field is string expression bound to virtual field $field  | 
            ||
| 1502 | * If $field is null, returns an array of all model virtual fields  | 
            ||
| 1503 | * or false if none $field exist.  | 
            ||
| 1504 | */  | 
            ||
| 1505 | 	public function getVirtualField($field = null) { | 
            ||
| 1520 | |||
| 1521 | /**  | 
            ||
| 1522 | * Initializes the model for writing a new record, loading the default values  | 
            ||
| 1523 | * for those fields that are not defined in $data, and clearing previous validation errors.  | 
            ||
| 1524 | * Especially helpful for saving data in loops.  | 
            ||
| 1525 | *  | 
            ||
| 1526 | * @param boolean|array $data Optional data array to assign to the model after it is created. If null or false,  | 
            ||
| 1527 | * schema data defaults are not merged.  | 
            ||
| 1528 | * @param boolean $filterKey If true, overwrites any primary key input with an empty value  | 
            ||
| 1529 | * @return array The current Model::data; after merging $data and/or defaults from database  | 
            ||
| 1530 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-create-array-data-array  | 
            ||
| 1531 | */  | 
            ||
| 1532 | 	public function create($data = array(), $filterKey = false) { | 
            ||
| 1556 | |||
| 1557 | /**  | 
            ||
| 1558 | * This function is a convenient wrapper class to create(false) and, as the name suggests, clears the id, data, and validation errors.  | 
            ||
| 1559 | *  | 
            ||
| 1560 | * @return boolean Always true upon success  | 
            ||
| 1561 | * @see Model::create()  | 
            ||
| 1562 | */  | 
            ||
| 1563 | 	public function clear() { | 
            ||
| 1567 | |||
| 1568 | /**  | 
            ||
| 1569 | * Returns a list of fields from the database, and sets the current model  | 
            ||
| 1570 | * data (Model::$data) with the record found.  | 
            ||
| 1571 | *  | 
            ||
| 1572 | * @param string|array $fields String of single field name, or an array of field names.  | 
            ||
| 1573 | * @param integer|string $id The ID of the record to read  | 
            ||
| 1574 | * @return array Array of database fields, or false if not found  | 
            ||
| 1575 | * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-read  | 
            ||
| 1576 | */  | 
            ||
| 1577 | 	public function read($fields = null, $id = null) { | 
            ||
| 1601 | |||
| 1602 | /**  | 
            ||
| 1603 | * Returns the contents of a single field given the supplied conditions, in the  | 
            ||
| 1604 | * supplied order.  | 
            ||
| 1605 | *  | 
            ||
| 1606 | * @param string $name Name of field to get  | 
            ||
| 1607 | * @param array $conditions SQL conditions (defaults to NULL)  | 
            ||
| 1608 | * @param string $order SQL ORDER BY fragment  | 
            ||
| 1609 | * @return string field contents, or false if not found  | 
            ||
| 1610 | * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-field  | 
            ||
| 1611 | */  | 
            ||
| 1612 | 	public function field($name, $conditions = null, $order = null) { | 
            ||
| 1643 | |||
| 1644 | /**  | 
            ||
| 1645 | * Saves the value of a single field to the database, based on the current  | 
            ||
| 1646 | * model ID.  | 
            ||
| 1647 | *  | 
            ||
| 1648 | * @param string $name Name of the table field  | 
            ||
| 1649 | * @param mixed $value Value of the field  | 
            ||
| 1650 | * @param boolean|array $validate Either a boolean, or an array.  | 
            ||
| 1651 | * If a boolean, indicates whether or not to validate before saving.  | 
            ||
| 1652 | * If an array, allows control of 'validate', 'callbacks' and 'counterCache' options.  | 
            ||
| 1653 | * See Model::save() for details of each options.  | 
            ||
| 1654 | * @return boolean See Model::save()  | 
            ||
| 1655 | * @see Model::save()  | 
            ||
| 1656 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savefield-string-fieldname-string-fieldvalue-validate-false  | 
            ||
| 1657 | */  | 
            ||
| 1658 | 	public function saveField($name, $value, $validate = false) { | 
            ||
| 1669 | |||
| 1670 | /**  | 
            ||
| 1671 | * Saves model data (based on white-list, if supplied) to the database. By  | 
            ||
| 1672 | * default, validation occurs before save.  | 
            ||
| 1673 | *  | 
            ||
| 1674 | * @param array $data Data to save.  | 
            ||
| 1675 | * @param boolean|array $validate Either a boolean, or an array.  | 
            ||
| 1676 | * If a boolean, indicates whether or not to validate before saving.  | 
            ||
| 1677 | * If an array, can have following keys:  | 
            ||
| 1678 | *  | 
            ||
| 1679 | * - validate: Set to true/false to enable or disable validation.  | 
            ||
| 1680 | * - fieldList: An array of fields you want to allow for saving.  | 
            ||
| 1681 | * - callbacks: Set to false to disable callbacks. Using 'before' or 'after'  | 
            ||
| 1682 | * will enable only those callbacks.  | 
            ||
| 1683 | * - `counterCache`: Boolean to control updating of counter caches (if any)  | 
            ||
| 1684 | *  | 
            ||
| 1685 | * @param array $fieldList List of fields to allow to be saved  | 
            ||
| 1686 | * @return mixed On success Model::$data if its not empty or true, false on failure  | 
            ||
| 1687 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html  | 
            ||
| 1688 | */  | 
            ||
| 1689 | 	public function save($data = null, $validate = true, $fieldList = array()) { | 
            ||
| 1876 | |||
| 1877 | /**  | 
            ||
| 1878 | * Check if the passed in field is a UUID field  | 
            ||
| 1879 | *  | 
            ||
| 1880 | * @param string $field the field to check  | 
            ||
| 1881 | * @return boolean  | 
            ||
| 1882 | */  | 
            ||
| 1883 | 	protected function _isUUIDField($field) { | 
            ||
| 1887 | |||
| 1888 | /**  | 
            ||
| 1889 | * Saves model hasAndBelongsToMany data to the database.  | 
            ||
| 1890 | *  | 
            ||
| 1891 | * @param array $joined Data to save  | 
            ||
| 1892 | * @param integer|string $id ID of record in this model  | 
            ||
| 1893 | * @param DataSource $db  | 
            ||
| 1894 | * @return void  | 
            ||
| 1895 | */  | 
            ||
| 1896 | 	protected function _saveMulti($joined, $id, $db) { | 
            ||
| 2019 | |||
| 2020 | /**  | 
            ||
| 2021 | * Updates the counter cache of belongsTo associations after a save or delete operation  | 
            ||
| 2022 | *  | 
            ||
| 2023 | * @param array $keys Optional foreign key data, defaults to the information $this->data  | 
            ||
| 2024 | * @param boolean $created True if a new record was created, otherwise only associations with  | 
            ||
| 2025 | * 'counterScope' defined get updated  | 
            ||
| 2026 | * @return void  | 
            ||
| 2027 | */  | 
            ||
| 2028 | 	public function updateCounterCache($keys = array(), $created = false) { | 
            ||
| 2098 | |||
| 2099 | /**  | 
            ||
| 2100 | * Helper method for `Model::updateCounterCache()`. Checks the fields to be updated for  | 
            ||
| 2101 | *  | 
            ||
| 2102 | * @param array $data The fields of the record that will be updated  | 
            ||
| 2103 | * @return array Returns updated foreign key values, along with an 'old' key containing the old  | 
            ||
| 2104 | * values, or empty if no foreign keys are updated.  | 
            ||
| 2105 | */  | 
            ||
| 2106 | 	protected function _prepareUpdateFields($data) { | 
            ||
| 2128 | |||
| 2129 | /**  | 
            ||
| 2130 | * Backwards compatible passthrough method for:  | 
            ||
| 2131 | * saveMany(), validateMany(), saveAssociated() and validateAssociated()  | 
            ||
| 2132 | *  | 
            ||
| 2133 | * Saves multiple individual records for a single model; Also works with a single record, as well as  | 
            ||
| 2134 | * all its associated records.  | 
            ||
| 2135 | *  | 
            ||
| 2136 | * #### Options  | 
            ||
| 2137 | *  | 
            ||
| 2138 | * - `validate`: Set to false to disable validation, true to validate each record before saving,  | 
            ||
| 2139 | * 'first' to validate *all* records before any are saved (default),  | 
            ||
| 2140 | * or 'only' to only validate the records, but not save them.  | 
            ||
| 2141 | * - `atomic`: If true (default), will attempt to save all records in a single transaction.  | 
            ||
| 2142 | * Should be set to false if database/table does not support transactions.  | 
            ||
| 2143 | * - `fieldList`: Equivalent to the $fieldList parameter in Model::save().  | 
            ||
| 2144 | * It should be an associate array with model name as key and array of fields as value. Eg.  | 
            ||
| 2145 |  *   {{{ | 
            ||
| 2146 | * array(  | 
            ||
| 2147 |  *       'SomeModel' => array('field'), | 
            ||
| 2148 |  *       'AssociatedModel' => array('field', 'otherfield') | 
            ||
| 2149 | * )  | 
            ||
| 2150 | * }}}  | 
            ||
| 2151 | * - `deep`: See saveMany/saveAssociated  | 
            ||
| 2152 | * - `callbacks`: See Model::save()  | 
            ||
| 2153 | * - `counterCache`: See Model::save()  | 
            ||
| 2154 | *  | 
            ||
| 2155 | * @param array $data Record data to save. This can be either a numerically-indexed array (for saving multiple  | 
            ||
| 2156 | * records of the same type), or an array indexed by association name.  | 
            ||
| 2157 | * @param array $options Options to use when saving record data, See $options above.  | 
            ||
| 2158 | * @return mixed If atomic: True on success, or false on failure.  | 
            ||
| 2159 | * Otherwise: array similar to the $data array passed, but values are set to true/false  | 
            ||
| 2160 | * depending on whether each record saved successfully.  | 
            ||
| 2161 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array  | 
            ||
| 2162 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array  | 
            ||
| 2163 | */  | 
            ||
| 2164 | 	public function saveAll($data = array(), $options = array()) { | 
            ||
| 2180 | |||
| 2181 | /**  | 
            ||
| 2182 | * Saves multiple individual records for a single model  | 
            ||
| 2183 | *  | 
            ||
| 2184 | * #### Options  | 
            ||
| 2185 | *  | 
            ||
| 2186 | * - `validate`: Set to false to disable validation, true to validate each record before saving,  | 
            ||
| 2187 | * 'first' to validate *all* records before any are saved (default),  | 
            ||
| 2188 | * - `atomic`: If true (default), will attempt to save all records in a single transaction.  | 
            ||
| 2189 | * Should be set to false if database/table does not support transactions.  | 
            ||
| 2190 | * - `fieldList`: Equivalent to the $fieldList parameter in Model::save()  | 
            ||
| 2191 | * - `deep`: If set to true, all associated data will be saved as well.  | 
            ||
| 2192 | * - `callbacks`: See Model::save()  | 
            ||
| 2193 | * - `counterCache`: See Model::save()  | 
            ||
| 2194 | *  | 
            ||
| 2195 | * @param array $data Record data to save. This should be a numerically-indexed array  | 
            ||
| 2196 | * @param array $options Options to use when saving record data, See $options above.  | 
            ||
| 2197 | * @return mixed If atomic: True on success, or false on failure.  | 
            ||
| 2198 | * Otherwise: array similar to the $data array passed, but values are set to true/false  | 
            ||
| 2199 | * depending on whether each record saved successfully.  | 
            ||
| 2200 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array  | 
            ||
| 2201 | */  | 
            ||
| 2202 | 	public function saveMany($data = null, $options = array()) { | 
            ||
| 2272 | |||
| 2273 | /**  | 
            ||
| 2274 | * Validates multiple individual records for a single model  | 
            ||
| 2275 | *  | 
            ||
| 2276 | * #### Options  | 
            ||
| 2277 | *  | 
            ||
| 2278 | * - `atomic`: If true (default), returns boolean. If false returns array.  | 
            ||
| 2279 | * - `fieldList`: Equivalent to the $fieldList parameter in Model::save()  | 
            ||
| 2280 | * - `deep`: If set to true, all associated data will be validated as well.  | 
            ||
| 2281 | *  | 
            ||
| 2282 | * Warning: This method could potentially change the passed argument `$data`,  | 
            ||
| 2283 | * If you do not want this to happen, make a copy of `$data` before passing it  | 
            ||
| 2284 | * to this method  | 
            ||
| 2285 | *  | 
            ||
| 2286 | * @param array $data Record data to validate. This should be a numerically-indexed array  | 
            ||
| 2287 | * @param array $options Options to use when validating record data (see above), See also $options of validates().  | 
            ||
| 2288 | * @return boolean|array If atomic: True on success, or false on failure.  | 
            ||
| 2289 | * Otherwise: array similar to the $data array passed, but values are set to true/false  | 
            ||
| 2290 | * depending on whether each record validated successfully.  | 
            ||
| 2291 | */  | 
            ||
| 2292 | 	public function validateMany(&$data, $options = array()) { | 
            ||
| 2295 | |||
| 2296 | /**  | 
            ||
| 2297 | * Saves a single record, as well as all its directly associated records.  | 
            ||
| 2298 | *  | 
            ||
| 2299 | * #### Options  | 
            ||
| 2300 | *  | 
            ||
| 2301 | * - `validate`: Set to `false` to disable validation, `true` to validate each record before saving,  | 
            ||
| 2302 | * 'first' to validate *all* records before any are saved(default),  | 
            ||
| 2303 | * - `atomic`: If true (default), will attempt to save all records in a single transaction.  | 
            ||
| 2304 | * Should be set to false if database/table does not support transactions.  | 
            ||
| 2305 | * - `fieldList`: Equivalent to the $fieldList parameter in Model::save().  | 
            ||
| 2306 | * It should be an associate array with model name as key and array of fields as value. Eg.  | 
            ||
| 2307 |  *   {{{ | 
            ||
| 2308 | * array(  | 
            ||
| 2309 |  *       'SomeModel' => array('field'), | 
            ||
| 2310 |  *       'AssociatedModel' => array('field', 'otherfield') | 
            ||
| 2311 | * )  | 
            ||
| 2312 | * }}}  | 
            ||
| 2313 | * - `deep`: If set to true, not only directly associated data is saved, but deeper nested associated data as well.  | 
            ||
| 2314 | * - `callbacks`: See Model::save()  | 
            ||
| 2315 | * - `counterCache`: See Model::save()  | 
            ||
| 2316 | *  | 
            ||
| 2317 | * @param array $data Record data to save. This should be an array indexed by association name.  | 
            ||
| 2318 | * @param array $options Options to use when saving record data, See $options above.  | 
            ||
| 2319 | * @return mixed If atomic: True on success, or false on failure.  | 
            ||
| 2320 | * Otherwise: array similar to the $data array passed, but values are set to true/false  | 
            ||
| 2321 | * depending on whether each record saved successfully.  | 
            ||
| 2322 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array  | 
            ||
| 2323 | */  | 
            ||
| 2324 | 	public function saveAssociated($data = null, $options = array()) { | 
            ||
| 2481 | |||
| 2482 | /**  | 
            ||
| 2483 | * Helper method for saveAll() and friends, to add foreign key to fieldlist  | 
            ||
| 2484 | *  | 
            ||
| 2485 | * @param string $key fieldname to be added to list  | 
            ||
| 2486 | * @param array $options  | 
            ||
| 2487 | * @return array $options  | 
            ||
| 2488 | */  | 
            ||
| 2489 | 	protected function _addToWhiteList($key, $options) { | 
            ||
| 2507 | |||
| 2508 | /**  | 
            ||
| 2509 | * Validates a single record, as well as all its directly associated records.  | 
            ||
| 2510 | *  | 
            ||
| 2511 | * #### Options  | 
            ||
| 2512 | *  | 
            ||
| 2513 | * - `atomic`: If true (default), returns boolean. If false returns array.  | 
            ||
| 2514 | * - `fieldList`: Equivalent to the $fieldList parameter in Model::save()  | 
            ||
| 2515 | * - `deep`: If set to true, not only directly associated data , but deeper nested associated data is validated as well.  | 
            ||
| 2516 | *  | 
            ||
| 2517 | * Warning: This method could potentially change the passed argument `$data`,  | 
            ||
| 2518 | * If you do not want this to happen, make a copy of `$data` before passing it  | 
            ||
| 2519 | * to this method  | 
            ||
| 2520 | *  | 
            ||
| 2521 | * @param array $data Record data to validate. This should be an array indexed by association name.  | 
            ||
| 2522 | * @param array $options Options to use when validating record data (see above), See also $options of validates().  | 
            ||
| 2523 | * @return array|boolean If atomic: True on success, or false on failure.  | 
            ||
| 2524 | * Otherwise: array similar to the $data array passed, but values are set to true/false  | 
            ||
| 2525 | * depending on whether each record validated successfully.  | 
            ||
| 2526 | */  | 
            ||
| 2527 | 	public function validateAssociated(&$data, $options = array()) { | 
            ||
| 2530 | |||
| 2531 | /**  | 
            ||
| 2532 | * Updates multiple model records based on a set of conditions.  | 
            ||
| 2533 | *  | 
            ||
| 2534 | * @param array $fields Set of fields and values, indexed by fields.  | 
            ||
| 2535 | * Fields are treated as SQL snippets, to insert literal values manually escape your data.  | 
            ||
| 2536 | * @param mixed $conditions Conditions to match, true for all records  | 
            ||
| 2537 | * @return boolean True on success, false on failure  | 
            ||
| 2538 | * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions  | 
            ||
| 2539 | */  | 
            ||
| 2540 | 	public function updateAll($fields, $conditions = true) { | 
            ||
| 2543 | |||
| 2544 | /**  | 
            ||
| 2545 | * Removes record for given ID. If no ID is given, the current ID is used. Returns true on success.  | 
            ||
| 2546 | *  | 
            ||
| 2547 | * @param integer|string $id ID of record to delete  | 
            ||
| 2548 | * @param boolean $cascade Set to true to delete records that depend on this record  | 
            ||
| 2549 | * @return boolean True on success  | 
            ||
| 2550 | * @link http://book.cakephp.org/2.0/en/models/deleting-data.html  | 
            ||
| 2551 | */  | 
            ||
| 2552 | 	public function delete($id = null, $cascade = true) { | 
            ||
| 2604 | |||
| 2605 | /**  | 
            ||
| 2606 | * Cascades model deletes through associated hasMany and hasOne child records.  | 
            ||
| 2607 | *  | 
            ||
| 2608 | * @param string $id ID of record that was deleted  | 
            ||
| 2609 | * @param boolean $cascade Set to true to delete records that depend on this record  | 
            ||
| 2610 | * @return void  | 
            ||
| 2611 | */  | 
            ||
| 2612 | 	protected function _deleteDependent($id, $cascade) { | 
            ||
| 2659 | |||
| 2660 | /**  | 
            ||
| 2661 | * Cascades model deletes through HABTM join keys.  | 
            ||
| 2662 | *  | 
            ||
| 2663 | * @param string $id ID of record that was deleted  | 
            ||
| 2664 | * @return void  | 
            ||
| 2665 | */  | 
            ||
| 2666 | 	protected function _deleteLinks($id) { | 
            ||
| 2684 | |||
| 2685 | /**  | 
            ||
| 2686 | * Deletes multiple model records based on a set of conditions.  | 
            ||
| 2687 | *  | 
            ||
| 2688 | * @param mixed $conditions Conditions to match  | 
            ||
| 2689 | * @param boolean $cascade Set to true to delete records that depend on this record  | 
            ||
| 2690 | * @param boolean $callbacks Run callbacks  | 
            ||
| 2691 | * @return boolean True on success, false on failure  | 
            ||
| 2692 | * @link http://book.cakephp.org/2.0/en/models/deleting-data.html#deleteall  | 
            ||
| 2693 | */  | 
            ||
| 2694 | 	public function deleteAll($conditions, $cascade = true, $callbacks = false) { | 
            ||
| 2741 | |||
| 2742 | /**  | 
            ||
| 2743 | * Collects foreign keys from associations.  | 
            ||
| 2744 | *  | 
            ||
| 2745 | * @param string $type  | 
            ||
| 2746 | * @return array  | 
            ||
| 2747 | */  | 
            ||
| 2748 | 	protected function _collectForeignKeys($type = 'belongsTo') { | 
            ||
| 2759 | |||
| 2760 | /**  | 
            ||
| 2761 | * Returns true if a record with particular ID exists.  | 
            ||
| 2762 | *  | 
            ||
| 2763 | * If $id is not passed it calls `Model::getID()` to obtain the current record ID,  | 
            ||
| 2764 |  * and then performs a `Model::find('count')` on the currently configured datasource | 
            ||
| 2765 | * to ascertain the existence of the record in persistent storage.  | 
            ||
| 2766 | *  | 
            ||
| 2767 | * @param integer|string $id ID of record to check for existence  | 
            ||
| 2768 | * @return boolean True if such a record exists  | 
            ||
| 2769 | */  | 
            ||
| 2770 | 	public function exists($id = null) { | 
            ||
| 2787 | |||
| 2788 | /**  | 
            ||
| 2789 | * Returns true if a record that meets given conditions exists.  | 
            ||
| 2790 | *  | 
            ||
| 2791 | * @param array $conditions SQL conditions array  | 
            ||
| 2792 | * @return boolean True if such a record exists  | 
            ||
| 2793 | */  | 
            ||
| 2794 | 	public function hasAny($conditions = null) { | 
            ||
| 2797 | |||
| 2798 | /**  | 
            ||
| 2799 | * Queries the datasource and returns a result set array.  | 
            ||
| 2800 | *  | 
            ||
| 2801 | * Used to perform find operations, where the first argument is type of find operation to perform  | 
            ||
| 2802 | * (all / first / count / neighbors / list / threaded),  | 
            ||
| 2803 | * second parameter options for finding (indexed array, including: 'conditions', 'limit',  | 
            ||
| 2804 | * 'recursive', 'page', 'fields', 'offset', 'order', 'callbacks')  | 
            ||
| 2805 | *  | 
            ||
| 2806 | * Eg:  | 
            ||
| 2807 |  * {{{ | 
            ||
| 2808 |  * $model->find('all', array( | 
            ||
| 2809 |  *   'conditions' => array('name' => 'Thomas Anderson'), | 
            ||
| 2810 |  *   'fields' => array('name', 'email'), | 
            ||
| 2811 | * 'order' => 'field3 DESC',  | 
            ||
| 2812 | * 'recursive' => 2,  | 
            ||
| 2813 | * 'group' => 'type',  | 
            ||
| 2814 | * 'callbacks' => false,  | 
            ||
| 2815 | * ));  | 
            ||
| 2816 | * }}}  | 
            ||
| 2817 | *  | 
            ||
| 2818 | * In addition to the standard query keys above, you can provide Datasource, and behavior specific  | 
            ||
| 2819 | * keys. For example, when using a SQL based datasource you can use the joins key to specify additional  | 
            ||
| 2820 | * joins that should be part of the query.  | 
            ||
| 2821 | *  | 
            ||
| 2822 |  * {{{ | 
            ||
| 2823 |  * $model->find('all', array( | 
            ||
| 2824 |  *   'conditions' => array('name' => 'Thomas Anderson'), | 
            ||
| 2825 | * 'joins' => array(  | 
            ||
| 2826 | * array(  | 
            ||
| 2827 | * 'alias' => 'Thought',  | 
            ||
| 2828 | * 'table' => 'thoughts',  | 
            ||
| 2829 | * 'type' => 'LEFT',  | 
            ||
| 2830 | * 'conditions' => '`Thought`.`person_id` = `Person`.`id`'  | 
            ||
| 2831 | * )  | 
            ||
| 2832 | * )  | 
            ||
| 2833 | * ));  | 
            ||
| 2834 | * }}}  | 
            ||
| 2835 | *  | 
            ||
| 2836 | * ### Disabling callbacks  | 
            ||
| 2837 | *  | 
            ||
| 2838 | * The `callbacks` key allows you to disable or specify the callbacks that should be run. To  | 
            ||
| 2839 | * disable beforeFind & afterFind callbacks set `'callbacks' => false` in your options. You can  | 
            ||
| 2840 | * also set the callbacks option to 'before' or 'after' to enable only the specified callback.  | 
            ||
| 2841 | *  | 
            ||
| 2842 | * ### Adding new find types  | 
            ||
| 2843 | *  | 
            ||
| 2844 | * Behaviors and find types can also define custom finder keys which are passed into find().  | 
            ||
| 2845 | * See the documentation for custom find types  | 
            ||
| 2846 | * (http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types)  | 
            ||
| 2847 | * for how to implement custom find types.  | 
            ||
| 2848 | *  | 
            ||
| 2849 | * Specifying 'fields' for notation 'list':  | 
            ||
| 2850 | *  | 
            ||
| 2851 | * - If no fields are specified, then 'id' is used for key and 'model->displayField' is used for value.  | 
            ||
| 2852 | * - If a single field is specified, 'id' is used for key and specified field is used for value.  | 
            ||
| 2853 | * - If three fields are specified, they are used (in order) for key, value and group.  | 
            ||
| 2854 | * - Otherwise, first and second fields are used for key and value.  | 
            ||
| 2855 | *  | 
            ||
| 2856 | * Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you  | 
            ||
| 2857 | * have issues with database views.  | 
            ||
| 2858 | *  | 
            ||
| 2859 | * Note: find(count) has its own return values.  | 
            ||
| 2860 | *  | 
            ||
| 2861 | * @param string $type Type of find operation (all / first / count / neighbors / list / threaded)  | 
            ||
| 2862 | * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks)  | 
            ||
| 2863 | * @return array Array of records, or Null on failure.  | 
            ||
| 2864 | * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html  | 
            ||
| 2865 | */  | 
            ||
| 2866 | 	public function find($type = 'first', $query = array()) { | 
            ||
| 2877 | |||
| 2878 | /**  | 
            ||
| 2879 | * Read from the datasource  | 
            ||
| 2880 | *  | 
            ||
| 2881 | * Model::_readDataSource() is used by all find() calls to read from the data source and can be overloaded to allow  | 
            ||
| 2882 | * caching of datasource calls.  | 
            ||
| 2883 | *  | 
            ||
| 2884 |  * {{{ | 
            ||
| 2885 |  * protected function _readDataSource($type, $query) { | 
            ||
| 2886 | * $cacheName = md5(json_encode($query));  | 
            ||
| 2887 | * $cache = Cache::read($cacheName, 'cache-config-name');  | 
            ||
| 2888 |  * 		if ($cache !== false) { | 
            ||
| 2889 | * return $cache;  | 
            ||
| 2890 | * }  | 
            ||
| 2891 | *  | 
            ||
| 2892 | * $results = parent::_readDataSource($type, $query);  | 
            ||
| 2893 | * Cache::write($cacheName, $results, 'cache-config-name');  | 
            ||
| 2894 | * return $results;  | 
            ||
| 2895 | * }  | 
            ||
| 2896 | * }}}  | 
            ||
| 2897 | *  | 
            ||
| 2898 | * @param string $type Type of find operation (all / first / count / neighbors / list / threaded)  | 
            ||
| 2899 | * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks)  | 
            ||
| 2900 | * @return array  | 
            ||
| 2901 | */  | 
            ||
| 2902 | 	protected function _readDataSource($type, $query) { | 
            ||
| 2916 | |||
| 2917 | /**  | 
            ||
| 2918 | * Builds the query array that is used by the data source to generate the query to fetch the data.  | 
            ||
| 2919 | *  | 
            ||
| 2920 | * @param string $type Type of find operation (all / first / count / neighbors / list / threaded)  | 
            ||
| 2921 | * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks)  | 
            ||
| 2922 | * @return array Query array or null if it could not be build for some reasons  | 
            ||
| 2923 | * @see Model::find()  | 
            ||
| 2924 | */  | 
            ||
| 2925 | 	public function buildQuery($type = 'first', $query = array()) { | 
            ||
| 2966 | |||
| 2967 | /**  | 
            ||
| 2968 |  * Handles the before/after filter logic for find('all') operations. Only called by Model::find(). | 
            ||
| 2969 | *  | 
            ||
| 2970 | * @param string $state Either "before" or "after"  | 
            ||
| 2971 | * @param array $query  | 
            ||
| 2972 | * @param array $results  | 
            ||
| 2973 | * @return array  | 
            ||
| 2974 | * @see Model::find()  | 
            ||
| 2975 | */  | 
            ||
| 2976 | 	protected function _findAll($state, $query, $results = array()) { | 
            ||
| 2983 | |||
| 2984 | /**  | 
            ||
| 2985 |  * Handles the before/after filter logic for find('first') operations. Only called by Model::find(). | 
            ||
| 2986 | *  | 
            ||
| 2987 | * @param string $state Either "before" or "after"  | 
            ||
| 2988 | * @param array $query  | 
            ||
| 2989 | * @param array $results  | 
            ||
| 2990 | * @return array  | 
            ||
| 2991 | * @see Model::find()  | 
            ||
| 2992 | */  | 
            ||
| 2993 | 	protected function _findFirst($state, $query, $results = array()) { | 
            ||
| 3005 | |||
| 3006 | /**  | 
            ||
| 3007 |  * Handles the before/after filter logic for find('count') operations. Only called by Model::find(). | 
            ||
| 3008 | *  | 
            ||
| 3009 | * @param string $state Either "before" or "after"  | 
            ||
| 3010 | * @param array $query  | 
            ||
| 3011 | * @param array $results  | 
            ||
| 3012 | * @return integer The number of records found, or false  | 
            ||
| 3013 | * @see Model::find()  | 
            ||
| 3014 | */  | 
            ||
| 3015 | 	protected function _findCount($state, $query, $results = array()) { | 
            ||
| 3057 | |||
| 3058 | /**  | 
            ||
| 3059 |  * Handles the before/after filter logic for find('list') operations. Only called by Model::find(). | 
            ||
| 3060 | *  | 
            ||
| 3061 | * @param string $state Either "before" or "after"  | 
            ||
| 3062 | * @param array $query  | 
            ||
| 3063 | * @param array $results  | 
            ||
| 3064 | * @return array Key/value pairs of primary keys/display field values of all records found  | 
            ||
| 3065 | * @see Model::find()  | 
            ||
| 3066 | */  | 
            ||
| 3067 | 	protected function _findList($state, $query, $results = array()) { | 
            ||
| 3117 | |||
| 3118 | /**  | 
            ||
| 3119 | * Detects the previous field's value, then uses logic to find the 'wrapping'  | 
            ||
| 3120 | * rows and return them.  | 
            ||
| 3121 | *  | 
            ||
| 3122 | * @param string $state Either "before" or "after"  | 
            ||
| 3123 | * @param array $query  | 
            ||
| 3124 | * @param array $results  | 
            ||
| 3125 | * @return array  | 
            ||
| 3126 | */  | 
            ||
| 3127 | 	protected function _findNeighbors($state, $query, $results = array()) { | 
            ||
| 3179 | |||
| 3180 | /**  | 
            ||
| 3181 | * In the event of ambiguous results returned (multiple top level results, with different parent_ids)  | 
            ||
| 3182 | * top level results with different parent_ids to the first result will be dropped  | 
            ||
| 3183 | *  | 
            ||
| 3184 | * @param string $state  | 
            ||
| 3185 | * @param mixed $query  | 
            ||
| 3186 | * @param array $results  | 
            ||
| 3187 | * @return array Threaded results  | 
            ||
| 3188 | */  | 
            ||
| 3189 | 	protected function _findThreaded($state, $query, $results = array()) { | 
            ||
| 3204 | |||
| 3205 | /**  | 
            ||
| 3206 | * Passes query results through model and behavior afterFind() methods.  | 
            ||
| 3207 | *  | 
            ||
| 3208 | * @param array $results Results to filter  | 
            ||
| 3209 | * @param boolean $primary If this is the primary model results (results from model where the find operation was performed)  | 
            ||
| 3210 | * @return array Set of filtered results  | 
            ||
| 3211 | */  | 
            ||
| 3212 | 	protected function _filterResults($results, $primary = true) { | 
            ||
| 3218 | |||
| 3219 | /**  | 
            ||
| 3220 | * This resets the association arrays for the model back  | 
            ||
| 3221 | * to those originally defined in the model. Normally called at the end  | 
            ||
| 3222 | * of each call to Model::find()  | 
            ||
| 3223 | *  | 
            ||
| 3224 | * @return boolean Success  | 
            ||
| 3225 | */  | 
            ||
| 3226 | 	public function resetAssociations() { | 
            ||
| 3248 | |||
| 3249 | /**  | 
            ||
| 3250 | * Returns false if any fields passed match any (by default, all if $or = false) of their matching values.  | 
            ||
| 3251 | *  | 
            ||
| 3252 | * @param array $fields Field/value pairs to search (if no values specified, they are pulled from $this->data)  | 
            ||
| 3253 | * @param boolean $or If false, all fields specified must match in order for a false return value  | 
            ||
| 3254 | * @return boolean False if any records matching any fields are found  | 
            ||
| 3255 | */  | 
            ||
| 3256 | 	public function isUnique($fields, $or = true) { | 
            ||
| 3292 | |||
| 3293 | /**  | 
            ||
| 3294 | * Returns a resultset for a given SQL statement. Custom SQL queries should be performed with this method.  | 
            ||
| 3295 | *  | 
            ||
| 3296 | * @param string $sql SQL statement  | 
            ||
| 3297 | * @param boolean|array $params Either a boolean to control query caching or an array of parameters  | 
            ||
| 3298 | * for use with prepared statement placeholders.  | 
            ||
| 3299 | * @param boolean $cache If $params is provided, a boolean flag for enabling/disabled  | 
            ||
| 3300 | * query caching.  | 
            ||
| 3301 | * @return mixed Resultset array or boolean indicating success / failure depending on the query executed  | 
            ||
| 3302 | * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-query  | 
            ||
| 3303 | */  | 
            ||
| 3304 | 	public function query($sql) { | 
            ||
| 3309 | |||
| 3310 | /**  | 
            ||
| 3311 | * Returns true if all fields pass validation. Will validate hasAndBelongsToMany associations  | 
            ||
| 3312 | * that use the 'with' key as well. Since _saveMulti is incapable of exiting a save operation.  | 
            ||
| 3313 | *  | 
            ||
| 3314 | * Will validate the currently set data. Use Model::set() or Model::create() to set the active data.  | 
            ||
| 3315 | *  | 
            ||
| 3316 | * @param array $options An optional array of custom options to be made available in the beforeValidate callback  | 
            ||
| 3317 | * @return boolean True if there are no errors  | 
            ||
| 3318 | */  | 
            ||
| 3319 | 	public function validates($options = array()) { | 
            ||
| 3322 | |||
| 3323 | /**  | 
            ||
| 3324 | * Returns an array of fields that have failed the validation of the current model.  | 
            ||
| 3325 | *  | 
            ||
| 3326 | * Additionally it populates the validationErrors property of the model with the same array.  | 
            ||
| 3327 | *  | 
            ||
| 3328 | * @param array|string $options An optional array of custom options to be made available in the beforeValidate callback  | 
            ||
| 3329 | * @return array Array of invalid fields and their error messages  | 
            ||
| 3330 | * @see Model::validates()  | 
            ||
| 3331 | */  | 
            ||
| 3332 | 	public function invalidFields($options = array()) { | 
            ||
| 3335 | |||
| 3336 | /**  | 
            ||
| 3337 | * Marks a field as invalid, optionally setting the name of validation  | 
            ||
| 3338 | * rule (in case of multiple validation for field) that was broken.  | 
            ||
| 3339 | *  | 
            ||
| 3340 | * @param string $field The name of the field to invalidate  | 
            ||
| 3341 | * @param mixed $value Name of validation rule that was not failed, or validation message to  | 
            ||
| 3342 | * be returned. If no validation key is provided, defaults to true.  | 
            ||
| 3343 | * @return void  | 
            ||
| 3344 | */  | 
            ||
| 3345 | 	public function invalidate($field, $value = true) { | 
            ||
| 3348 | |||
| 3349 | /**  | 
            ||
| 3350 | * Returns true if given field name is a foreign key in this model.  | 
            ||
| 3351 | *  | 
            ||
| 3352 | * @param string $field Returns true if the input string ends in "_id"  | 
            ||
| 3353 | * @return boolean True if the field is a foreign key listed in the belongsTo array.  | 
            ||
| 3354 | */  | 
            ||
| 3355 | 	public function isForeignKey($field) { | 
            ||
| 3365 | |||
| 3366 | /**  | 
            ||
| 3367 | * Escapes the field name and prepends the model name. Escaping is done according to the  | 
            ||
| 3368 | * current database driver's rules.  | 
            ||
| 3369 | *  | 
            ||
| 3370 | * @param string $field Field to escape (e.g: id)  | 
            ||
| 3371 | * @param string $alias Alias for the model (e.g: Post)  | 
            ||
| 3372 | * @return string The name of the escaped field for this Model (i.e. id becomes `Post`.`id`).  | 
            ||
| 3373 | */  | 
            ||
| 3374 | 	public function escapeField($field = null, $alias = null) { | 
            ||
| 3390 | |||
| 3391 | /**  | 
            ||
| 3392 | * Returns the current record's ID  | 
            ||
| 3393 | *  | 
            ||
| 3394 | * @param integer $list Index on which the composed ID is located  | 
            ||
| 3395 | * @return mixed The ID of the current record, false if no ID  | 
            ||
| 3396 | */  | 
            ||
| 3397 | 	public function getID($list = 0) { | 
            ||
| 3416 | |||
| 3417 | /**  | 
            ||
| 3418 | * Returns the ID of the last record this model inserted.  | 
            ||
| 3419 | *  | 
            ||
| 3420 | * @return mixed Last inserted ID  | 
            ||
| 3421 | */  | 
            ||
| 3422 | 	public function getLastInsertID() { | 
            ||
| 3425 | |||
| 3426 | /**  | 
            ||
| 3427 | * Returns the ID of the last record this model inserted.  | 
            ||
| 3428 | *  | 
            ||
| 3429 | * @return mixed Last inserted ID  | 
            ||
| 3430 | */  | 
            ||
| 3431 | 	public function getInsertID() { | 
            ||
| 3434 | |||
| 3435 | /**  | 
            ||
| 3436 | * Sets the ID of the last record this model inserted  | 
            ||
| 3437 | *  | 
            ||
| 3438 | * @param integer|string $id Last inserted ID  | 
            ||
| 3439 | * @return void  | 
            ||
| 3440 | */  | 
            ||
| 3441 | 	public function setInsertID($id) { | 
            ||
| 3444 | |||
| 3445 | /**  | 
            ||
| 3446 | * Returns the number of rows returned from the last query.  | 
            ||
| 3447 | *  | 
            ||
| 3448 | * @return integer Number of rows  | 
            ||
| 3449 | */  | 
            ||
| 3450 | 	public function getNumRows() { | 
            ||
| 3453 | |||
| 3454 | /**  | 
            ||
| 3455 | * Returns the number of rows affected by the last query.  | 
            ||
| 3456 | *  | 
            ||
| 3457 | * @return integer Number of rows  | 
            ||
| 3458 | */  | 
            ||
| 3459 | 	public function getAffectedRows() { | 
            ||
| 3462 | |||
| 3463 | /**  | 
            ||
| 3464 | * Sets the DataSource to which this model is bound.  | 
            ||
| 3465 | *  | 
            ||
| 3466 | * @param string $dataSource The name of the DataSource, as defined in app/Config/database.php  | 
            ||
| 3467 | * @return void  | 
            ||
| 3468 | * @throws MissingConnectionException  | 
            ||
| 3469 | */  | 
            ||
| 3470 | 	public function setDataSource($dataSource = null) { | 
            ||
| 3490 | |||
| 3491 | /**  | 
            ||
| 3492 | * Gets the DataSource to which this model is bound.  | 
            ||
| 3493 | *  | 
            ||
| 3494 | * @return DataSource A DataSource object  | 
            ||
| 3495 | */  | 
            ||
| 3496 | 	public function getDataSource() { | 
            ||
| 3504 | |||
| 3505 | /**  | 
            ||
| 3506 | * Get associations  | 
            ||
| 3507 | *  | 
            ||
| 3508 | * @return array  | 
            ||
| 3509 | */  | 
            ||
| 3510 | 	public function associations() { | 
            ||
| 3513 | |||
| 3514 | /**  | 
            ||
| 3515 | * Gets all the models with which this model is associated.  | 
            ||
| 3516 | *  | 
            ||
| 3517 | * @param string $type Only result associations of this type  | 
            ||
| 3518 | * @return array Associations  | 
            ||
| 3519 | */  | 
            ||
| 3520 | 	public function getAssociated($type = null) { | 
            ||
| 3563 | |||
| 3564 | /**  | 
            ||
| 3565 | * Gets the name and fields to be used by a join model. This allows specifying join fields  | 
            ||
| 3566 | * in the association definition.  | 
            ||
| 3567 | *  | 
            ||
| 3568 | * @param string|array $assoc The model to be joined  | 
            ||
| 3569 | * @param array $keys Any join keys which must be merged with the keys queried  | 
            ||
| 3570 | * @return array  | 
            ||
| 3571 | */  | 
            ||
| 3572 | 	public function joinModel($assoc, $keys = array()) { | 
            ||
| 3588 | |||
| 3589 | /**  | 
            ||
| 3590 | * Called before each find operation. Return false if you want to halt the find  | 
            ||
| 3591 | * call, otherwise return the (modified) query data.  | 
            ||
| 3592 | *  | 
            ||
| 3593 | * @param array $query Data used to execute this query, i.e. conditions, order, etc.  | 
            ||
| 3594 | * @return mixed true if the operation should continue, false if it should abort; or, modified  | 
            ||
| 3595 | * $query to continue with new $query  | 
            ||
| 3596 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforefind  | 
            ||
| 3597 | */  | 
            ||
| 3598 | 	public function beforeFind($query) { | 
            ||
| 3601 | |||
| 3602 | /**  | 
            ||
| 3603 | * Called after each find operation. Can be used to modify any results returned by find().  | 
            ||
| 3604 | * Return value should be the (modified) results.  | 
            ||
| 3605 | *  | 
            ||
| 3606 | * @param mixed $results The results of the find operation  | 
            ||
| 3607 | * @param boolean $primary Whether this model is being queried directly (vs. being queried as an association)  | 
            ||
| 3608 | * @return mixed Result of the find operation  | 
            ||
| 3609 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind  | 
            ||
| 3610 | */  | 
            ||
| 3611 | 	public function afterFind($results, $primary = false) { | 
            ||
| 3614 | |||
| 3615 | /**  | 
            ||
| 3616 | * Called before each save operation, after validation. Return a non-true result  | 
            ||
| 3617 | * to halt the save.  | 
            ||
| 3618 | *  | 
            ||
| 3619 | * @param array $options Options passed from Model::save().  | 
            ||
| 3620 | * @return boolean True if the operation should continue, false if it should abort  | 
            ||
| 3621 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforesave  | 
            ||
| 3622 | * @see Model::save()  | 
            ||
| 3623 | */  | 
            ||
| 3624 | 	public function beforeSave($options = array()) { | 
            ||
| 3627 | |||
| 3628 | /**  | 
            ||
| 3629 | * Called after each successful save operation.  | 
            ||
| 3630 | *  | 
            ||
| 3631 | * @param boolean $created True if this save created a new record  | 
            ||
| 3632 | * @param array $options Options passed from Model::save().  | 
            ||
| 3633 | * @return void  | 
            ||
| 3634 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#aftersave  | 
            ||
| 3635 | * @see Model::save()  | 
            ||
| 3636 | */  | 
            ||
| 3637 | 	public function afterSave($created, $options = array()) { | 
            ||
| 3639 | |||
| 3640 | /**  | 
            ||
| 3641 | * Called before every deletion operation.  | 
            ||
| 3642 | *  | 
            ||
| 3643 | * @param boolean $cascade If true records that depend on this record will also be deleted  | 
            ||
| 3644 | * @return boolean True if the operation should continue, false if it should abort  | 
            ||
| 3645 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforedelete  | 
            ||
| 3646 | */  | 
            ||
| 3647 | 	public function beforeDelete($cascade = true) { | 
            ||
| 3650 | |||
| 3651 | /**  | 
            ||
| 3652 | * Called after every deletion operation.  | 
            ||
| 3653 | *  | 
            ||
| 3654 | * @return void  | 
            ||
| 3655 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#afterdelete  | 
            ||
| 3656 | */  | 
            ||
| 3657 | 	public function afterDelete() { | 
            ||
| 3659 | |||
| 3660 | /**  | 
            ||
| 3661 | * Called during validation operations, before validation. Please note that custom  | 
            ||
| 3662 | * validation rules can be defined in $validate.  | 
            ||
| 3663 | *  | 
            ||
| 3664 | * @param array $options Options passed from Model::save().  | 
            ||
| 3665 | * @return boolean True if validate operation should continue, false to abort  | 
            ||
| 3666 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate  | 
            ||
| 3667 | * @see Model::save()  | 
            ||
| 3668 | */  | 
            ||
| 3669 | 	public function beforeValidate($options = array()) { | 
            ||
| 3672 | |||
| 3673 | /**  | 
            ||
| 3674 | * Called after data has been checked for errors  | 
            ||
| 3675 | *  | 
            ||
| 3676 | * @return void  | 
            ||
| 3677 | */  | 
            ||
| 3678 | 	public function afterValidate() { | 
            ||
| 3680 | |||
| 3681 | /**  | 
            ||
| 3682 | * Called when a DataSource-level error occurs.  | 
            ||
| 3683 | *  | 
            ||
| 3684 | * @return void  | 
            ||
| 3685 | * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#onerror  | 
            ||
| 3686 | */  | 
            ||
| 3687 | 	public function onError() { | 
            ||
| 3689 | |||
| 3690 | /**  | 
            ||
| 3691 | * Clears cache for this model.  | 
            ||
| 3692 | *  | 
            ||
| 3693 | * @param string $type If null this deletes cached views if Cache.check is true  | 
            ||
| 3694 | * Will be used to allow deleting query cache also  | 
            ||
| 3695 | * @return mixed True on delete, null otherwise  | 
            ||
| 3696 | */  | 
            ||
| 3697 | 	protected function _clearCache($type = null) { | 
            ||
| 3720 | |||
| 3721 | /**  | 
            ||
| 3722 | * Returns an instance of a model validator for this class  | 
            ||
| 3723 | *  | 
            ||
| 3724 | * @param ModelValidator Model validator instance.  | 
            ||
| 3725 | * If null a new ModelValidator instance will be made using current model object  | 
            ||
| 3726 | * @return ModelValidator  | 
            ||
| 3727 | */  | 
            ||
| 3728 | 	public function validator(ModelValidator $instance = null) { | 
            ||
| 3737 | |||
| 3738 | }  | 
            ||
| 3739 | 
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.