2005-08-21 06:49:02 +00:00
< ? php
/* SVN FILE: $Id$ */
2005-06-23 13:59:02 +00:00
/**
2005-09-07 01:52:45 +00:00
* Object - relational mapper .
2005-12-22 01:07:28 +00:00
*
2005-09-07 01:52:45 +00:00
* DBO - backed object data model , for mapping database tables to Cake objects .
2005-06-23 13:59:02 +00:00
*
2006-07-30 12:24:03 +00:00
* PHP versions 5
2005-08-21 06:49:02 +00:00
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
2008-01-01 22:18:17 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2006-05-26 05:29:17 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2005-12-27 03:33:44 +00:00
*
2005-12-23 21:57:26 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
2005-06-23 13:59:02 +00:00
*
2005-12-22 01:07:28 +00:00
* @ filesource
2008-01-01 22:18:17 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-02-02 10:39:45 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake . libs . model
2007-02-02 10:39:45 +00:00
* @ since CakePHP ( tm ) v 0.10 . 0.0
2006-05-26 05:29:17 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2005-06-23 13:59:02 +00:00
*/
2006-07-22 14:13:07 +00:00
/**
2006-07-30 12:24:03 +00:00
* Included libs
2006-07-22 14:13:07 +00:00
*/
2007-11-18 07:18:25 +00:00
App :: import ( 'Core' , array ( 'ClassRegistry' , 'Overloadable' , 'Validation' , 'Behavior' , 'ConnectionManager' , 'Set' ));
2006-01-17 17:52:23 +00:00
/**
2006-07-30 12:24:03 +00:00
* Object - relational mapper .
*
* DBO - backed object data model .
* Automatically selects a database table name based on a pluralized lowercase object class name
* ( i . e . class ' User ' => table ' users '; class ' Man ' => table ' men ' )
* The table is required to have at least 'id auto_increment' , 'created datetime' ,
* and 'modified datetime' fields .
*
* @ package cake
* @ subpackage cake . cake . libs . model
*/
class Model extends Overloadable {
/**
* The name of the DataSource connection that this Model uses
*
* @ var string
* @ access public
*/
var $useDbConfig = 'default' ;
/**
* Custom database table name .
*
* @ var string
* @ access public
*/
var $useTable = null ;
/**
* Custom display field name . Display fields are used by Scaffold , in SELECT boxes ' OPTION elements .
*
* @ var string
* @ access public
*/
var $displayField = null ;
/**
* Value of the primary key ID of the record that this model is currently pointing to
*
2007-11-05 03:36:12 +00:00
* @ var mixed
2006-07-30 12:24:03 +00:00
* @ access public
*/
var $id = false ;
/**
* Container for the data that this model gets from persistent storage ( the database ) .
*
* @ var array
* @ access public
*/
var $data = array ();
/**
* Table name for this Model .
*
* @ var string
* @ access public
*/
var $table = false ;
/**
* The name of the ID field for this Model .
*
* @ var string
* @ access public
*/
var $primaryKey = null ;
/**
* Table metadata
*
* @ var array
2007-08-21 21:46:59 +00:00
* @ access protected
*/
var $_schema = null ;
2006-07-30 12:24:03 +00:00
/**
* List of validation rules . Append entries for validation as ( 'field_name' => '/^perl_compat_regexp$/' )
* that have to match with preg_match () . Use these rules with Model :: validate ()
*
* @ var array
* @ access public
*/
var $validate = array ();
/**
* Errors in validation
2007-11-05 03:36:12 +00:00
*
2006-07-30 12:24:03 +00:00
* @ var array
* @ access public
*/
var $validationErrors = array ();
/**
* Database table prefix for tables in model .
*
* @ var string
* @ access public
*/
var $tablePrefix = null ;
/**
* Name of the model .
*
* @ var string
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
var $name = null ;
/**
2007-10-27 01:32:17 +00:00
* Alias name for model .
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* @ var string
2007-10-27 01:32:17 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-10-27 01:32:17 +00:00
var $alias = null ;
2006-07-30 12:24:03 +00:00
/**
* List of table names included in the Model description . Used for associations .
*
* @ var array
* @ access public
*/
var $tableToModel = array ();
/**
* Whether or not transactions for this model should be logged
*
* @ var boolean
* @ access public
*/
var $logTransactions = false ;
/**
* Whether or not to enable transactions for this model ( i . e . BEGIN / COMMIT / ROLLBACK )
*
* @ var boolean
* @ access public
*/
var $transactional = false ;
/**
* Whether or not to cache queries for this model . This enables in - memory
* caching only , the results are not stored beyond this execution .
*
* @ var boolean
* @ access public
*/
2006-11-21 22:40:30 +00:00
var $cacheQueries = false ;
2006-07-30 12:24:03 +00:00
/**
* belongsTo association
*
* @ var array
* @ access public
*/
var $belongsTo = array ();
/**
* hasOne association
*
* @ var array
* @ access public
*/
var $hasOne = array ();
/**
* hasMany association
*
* @ var array
* @ access public
*/
var $hasMany = array ();
/**
* hasAndBelongsToMany association
*
* @ var array
* @ access public
*/
var $hasAndBelongsToMany = array ();
/**
2008-02-01 02:03:04 +00:00
* List of behaviors to load when the model object is initialized . Settings can be
* passed to behaviors by using the behavior name as index . Eg :
2007-11-05 03:36:12 +00:00
*
* array ( 'Translate' , 'MyBehavior' => array ( 'setting1' => 'value1' ))
2006-07-30 12:24:03 +00:00
*
* @ var array
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
var $actsAs = null ;
/**
2008-02-21 15:36:13 +00:00
* Holds the Behavior objects currently bound to this model
2006-07-30 12:24:03 +00:00
*
2008-02-21 15:36:13 +00:00
* @ var object
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-02-21 15:36:13 +00:00
var $Behaviors = null ;
2007-10-17 21:19:30 +00:00
/**
* Whitelist of fields allowed to be saved
*
* @ var array
2007-11-05 03:36:12 +00:00
* @ access public
2007-10-17 21:19:30 +00:00
*/
var $whitelist = array ();
2007-04-08 23:30:31 +00:00
/**
2007-11-05 03:36:12 +00:00
* Should sources for this model be cached .
2007-04-08 23:30:31 +00:00
*
* @ var boolean
2007-11-05 03:36:12 +00:00
* @ access public
2007-04-08 23:30:31 +00:00
*/
var $cacheSources = true ;
2007-12-25 10:37:08 +00:00
/**
* Type of find query currently executing
*
* @ var string
* @ access public
*/
var $findQueryType = null ;
2006-07-30 12:24:03 +00:00
/**
* Depth of recursive association
*
* @ var integer
* @ access public
*/
var $recursive = 1 ;
/**
* Default ordering of model records
*
2007-11-05 03:36:12 +00:00
* @ var string
* @ access public
2006-07-30 12:24:03 +00:00
*/
var $order = null ;
2007-10-01 16:49:37 +00:00
/**
2007-11-05 03:36:12 +00:00
* Whether or not the model record exists , set by Model :: exists ()
2007-10-01 16:49:37 +00:00
*
* @ var bool
2007-11-05 03:36:12 +00:00
* @ access private
2007-10-01 16:49:37 +00:00
*/
var $__exists = null ;
2006-07-30 12:24:03 +00:00
/**
* Default association keys
*
* @ var array
2007-11-05 03:36:12 +00:00
* @ access private
2006-07-30 12:24:03 +00:00
*/
var $__associationKeys = array (
2007-10-27 05:29:11 +00:00
'belongsTo' => array ( 'className' , 'foreignKey' , 'conditions' , 'fields' , 'order' , 'counterCache' ),
'hasOne' => array ( 'className' , 'foreignKey' , 'conditions' , 'fields' , 'order' , 'dependent' ),
'hasMany' => array ( 'className' , 'foreignKey' , 'conditions' , 'fields' , 'order' , 'limit' , 'offset' , 'dependent' , 'exclusive' , 'finderQuery' , 'counterQuery' ),
'hasAndBelongsToMany' => array ( 'className' , 'joinTable' , 'with' , 'foreignKey' , 'associationForeignKey' , 'conditions' , 'fields' , 'order' , 'limit' , 'offset' , 'unique' , 'finderQuery' , 'deleteQuery' , 'insertQuery' ));
2006-07-30 12:24:03 +00:00
/**
* Holds provided / generated association key names and other data for all associations
*
* @ var array
2007-11-05 03:36:12 +00:00
* @ access private
2006-07-30 12:24:03 +00:00
*/
var $__associations = array ( 'belongsTo' , 'hasOne' , 'hasMany' , 'hasAndBelongsToMany' );
2007-03-26 17:16:43 +00:00
/**
2007-05-20 05:05:18 +00:00
* Holds model associations temporarily to allow for dynamic ( un ) binding
2007-03-26 17:16:43 +00:00
*
* @ var array
2007-05-20 05:05:18 +00:00
* @ access private
2007-03-26 17:16:43 +00:00
*/
var $__backAssociation = array ();
2006-07-30 12:24:03 +00:00
/**
* The last inserted ID of the data that this model created
*
* @ var integer
2007-11-05 03:36:12 +00:00
* @ access private
2006-07-30 12:24:03 +00:00
*/
var $__insertID = null ;
/**
* The number of records returned by the last query
*
* @ var integer
2007-11-05 03:36:12 +00:00
* @ access private
2006-07-30 12:24:03 +00:00
*/
var $__numRows = null ;
/**
* The number of records affected by the last query
*
* @ var integer
2007-11-05 03:36:12 +00:00
* @ access private
2006-07-30 12:24:03 +00:00
*/
var $__affectedRows = null ;
2007-10-21 18:34:23 +00:00
/**
* List of valid finder method options
*
* @ var array
2007-11-05 03:36:12 +00:00
* @ access private
2007-10-21 18:34:23 +00:00
*/
2008-01-01 19:34:40 +00:00
var $__findMethods = array ( 'all' => true , 'first' => true , 'count' => true , 'neighbors' => true , 'list' => true );
2006-07-30 12:24:03 +00:00
/**
* Constructor . Binds the Model ' s database table to the object .
*
2007-11-05 03:36:12 +00:00
* @ param integer $id Set this ID for this model on startup
2006-07-30 12:24:03 +00:00
* @ param string $table Name of database table to use .
2007-11-05 03:36:12 +00:00
* @ param object $ds DataSource connection object .
2006-07-30 12:24:03 +00:00
*/
function __construct ( $id = false , $table = null , $ds = null ) {
parent :: __construct ();
2008-05-13 16:42:39 +00:00
if ( is_array ( $id )) {
extract ( array_merge ( array ( 'id' => false , 'table' => null , 'ds' => null , 'name' => null , 'alias' => null ), $id ));
$this -> name = $name ;
$this -> alias = $alias ;
2007-08-04 17:20:50 +00:00
}
2006-07-30 12:24:03 +00:00
if ( $this -> name === null ) {
$this -> name = get_class ( $this );
}
2008-05-13 16:42:39 +00:00
if ( $this -> alias === null ) {
$this -> alias = $this -> name ;
}
2006-07-30 12:24:03 +00:00
if ( $this -> primaryKey === null ) {
$this -> primaryKey = 'id' ;
}
2007-10-27 01:32:17 +00:00
ClassRegistry :: addObject ( $this -> alias , $this );
2007-05-20 05:05:18 +00:00
2006-07-30 12:24:03 +00:00
$this -> id = $id ;
2007-10-22 02:11:58 +00:00
unset ( $id );
2007-05-20 05:05:18 +00:00
2007-06-20 06:15:35 +00:00
if ( $table === false ) {
2007-05-13 01:09:40 +00:00
$this -> useTable = false ;
2007-06-20 06:15:35 +00:00
} elseif ( $table ) {
2007-05-13 01:09:40 +00:00
$this -> useTable = $table ;
}
2007-05-20 05:05:18 +00:00
2006-07-30 12:24:03 +00:00
if ( $this -> useTable !== false ) {
$this -> setDataSource ( $ds );
2007-05-20 05:05:18 +00:00
2007-05-13 01:09:40 +00:00
if ( $this -> useTable === null ) {
$this -> useTable = Inflector :: tableize ( $this -> name );
2006-07-30 12:24:03 +00:00
}
if ( in_array ( 'settableprefix' , get_class_methods ( $this ))) {
$this -> setTablePrefix ();
}
2007-05-13 01:09:40 +00:00
$this -> setSource ( $this -> useTable );
2006-07-30 12:24:03 +00:00
$this -> __createLinks ();
if ( $this -> displayField == null ) {
2008-04-17 13:10:27 +00:00
$this -> displayField = $this -> hasField ( array ( 'title' , 'name' , $this -> primaryKey ));
2006-07-30 12:24:03 +00:00
}
}
2007-09-20 15:16:25 +00:00
if ( is_subclass_of ( $this , 'AppModel' )) {
$appVars = get_class_vars ( 'AppModel' );
2008-01-01 01:07:24 +00:00
$merge = array ();
2007-09-20 15:16:25 +00:00
if ( $this -> actsAs !== null || $this -> actsAs !== false ) {
$merge [] = 'actsAs' ;
}
foreach ( $merge as $var ) {
if ( isset ( $appVars [ $var ]) && ! empty ( $appVars [ $var ]) && is_array ( $this -> { $var })) {
2008-01-01 01:07:24 +00:00
$this -> { $var } = Set :: merge ( $appVars [ $var ], $this -> { $var });
2007-09-20 15:16:25 +00:00
}
}
}
2008-03-09 06:24:40 +00:00
$this -> Behaviors = new BehaviorCollection ();
2008-03-16 05:44:20 +00:00
$this -> Behaviors -> init ( $this -> alias , $this -> actsAs );
2006-07-30 12:24:03 +00:00
}
/**
* Handles custom method calls , like findBy < field > for DB models ,
* and custom RPC calls for remote data sources .
*
2007-11-05 03:36:12 +00:00
* @ param string $method Name of method to call .
* @ param array $params Parameters for the method .
* @ return mixed Whatever is returned by called method
2006-07-30 12:24:03 +00:00
* @ access protected
*/
2006-12-26 16:17:37 +00:00
function call__ ( $method , $params ) {
2008-03-09 06:24:40 +00:00
$result = $this -> Behaviors -> dispatchMethod ( $this , $method , $params );
2006-10-20 17:24:37 +00:00
2008-02-21 15:36:13 +00:00
if ( $result !== array ( 'unhandled' )) {
return $result ;
2006-07-30 12:24:03 +00:00
}
2007-12-19 02:05:15 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-11-27 05:57:53 +00:00
$return = $db -> query ( $method , $params , $this );
2006-12-19 19:26:02 +00:00
2006-11-27 05:57:53 +00:00
if ( ! PHP5 ) {
2008-05-17 16:23:46 +00:00
$this -> resetAssociations ();
2006-11-27 05:57:53 +00:00
}
return $return ;
2006-07-30 12:24:03 +00:00
}
2007-01-06 04:58:24 +00:00
/**
* Bind model associations on the fly .
*
2007-01-06 08:41:48 +00:00
* If $permanent is true , association will not be reset
2007-11-05 03:36:12 +00:00
* to the originals defined in the model .
2007-01-06 04:58:24 +00:00
*
2008-01-01 19:34:40 +00:00
* @ param mixed $model A model or association name ( string ) or set of binding options ( indexed by model name type )
* @ param array $options If $model is a string , this is the list of association properties with which $model will
* be bound
2007-11-05 03:36:12 +00:00
* @ param boolean $permanent Set to true to make the binding permanent
* @ access public
* @ todo
2007-01-06 04:58:24 +00:00
*/
2008-01-01 19:34:40 +00:00
function bind ( $model , $options = array (), $permanent = true ) {
2007-01-31 23:25:05 +00:00
if ( ! is_array ( $model )) {
$model = array ( $model => $options );
}
2007-06-20 06:15:35 +00:00
foreach ( $model as $name => $options ) {
2007-01-31 23:25:05 +00:00
if ( isset ( $options [ 'type' ])) {
$assoc = $options [ 'type' ];
} elseif ( isset ( $options [ 0 ])) {
$assoc = $options [ 0 ];
2008-01-01 19:34:40 +00:00
} else {
$assoc = 'belongsTo' ;
2007-01-31 23:25:05 +00:00
}
2008-01-01 19:34:40 +00:00
2007-06-20 06:15:35 +00:00
if ( ! $permanent ) {
2007-01-31 23:25:05 +00:00
$this -> __backAssociation [ $assoc ] = $this -> { $assoc };
}
2007-06-20 06:15:35 +00:00
foreach ( $model as $key => $value ) {
2008-01-01 19:34:40 +00:00
$assocName = $modelName = $key ;
2008-01-01 22:28:47 +00:00
2008-01-01 19:34:40 +00:00
if ( isset ( $this -> { $assoc }[ $assocName ])) {
$this -> { $assoc }[ $assocName ] = array_merge ( $this -> { $assoc }[ $assocName ], $options );
} else {
if ( isset ( $value [ 'className' ])) {
$modelName = $value [ 'className' ];
}
2007-01-31 23:25:05 +00:00
2008-01-01 19:34:40 +00:00
$this -> __constructLinkedModel ( $assocName , $modelName );
$this -> { $assoc }[ $assocName ] = $model [ $assocName ];
$this -> __generateAssociation ( $assoc );
2007-01-31 23:25:05 +00:00
}
2008-01-01 19:34:40 +00:00
unset ( $this -> { $assoc }[ $assocName ][ 'type' ], $this -> { $assoc }[ $assocName ][ 0 ]);
2007-01-31 23:25:05 +00:00
}
}
2007-01-06 04:58:24 +00:00
}
2006-07-30 12:24:03 +00:00
/**
* Bind model associations on the fly .
2006-12-28 04:02:38 +00:00
*
* If $reset is false , association will not be reset
* to the originals defined in the model
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* Example : Add a new hasOne binding to the Profile model not
* defined in the model source code :
* < code >
* $this -> User -> bindModel ( array ( 'hasOne' => array ( 'Profile' )) );
* </ code >
*
* @ param array $params Set of bindings ( indexed by binding type )
* @ param boolean $reset Set to false to make the binding permanent
* @ return boolean Success
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-02-01 18:45:38 +00:00
function bindModel ( $params , $reset = true ) {
2007-06-20 06:15:35 +00:00
foreach ( $params as $assoc => $model ) {
if ( $reset === true ) {
2007-02-01 18:45:38 +00:00
$this -> __backAssociation [ $assoc ] = $this -> { $assoc };
}
2006-07-30 12:24:03 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $model as $key => $value ) {
2006-07-30 12:24:03 +00:00
$assocName = $key ;
2007-02-01 18:37:42 +00:00
if ( is_numeric ( $key )) {
$assocName = $value ;
$value = array ();
2006-07-30 12:24:03 +00:00
}
2007-02-01 18:37:42 +00:00
$modelName = $assocName ;
$this -> { $assoc }[ $assocName ] = $value ;
2006-07-30 12:24:03 +00:00
}
}
2007-02-01 18:37:42 +00:00
$this -> __createLinks ();
2006-07-30 12:24:03 +00:00
return true ;
}
/**
* Turn off associations on the fly .
*
2006-12-28 04:02:38 +00:00
* If $reset is false , association will not be reset
* to the originals defined in the model
*
* Example : Turn off the associated Model Support request ,
2006-08-03 06:49:51 +00:00
* to temporarily lighten the User model :
* < code >
* $this -> User -> unbindModel ( array ( 'hasMany' => array ( 'Supportrequest' )) );
* </ code >
*
2007-11-05 03:36:12 +00:00
* @ param array $params Set of bindings to unbind ( indexed by binding type )
* @ param boolean $reset Set to false to make the unbinding permanent
* @ return boolean Success
* @ access public
2006-07-30 12:24:03 +00:00
*/
2006-12-28 04:02:38 +00:00
function unbindModel ( $params , $reset = true ) {
2007-06-20 06:15:35 +00:00
foreach ( $params as $assoc => $models ) {
if ( $reset === true ) {
2006-12-28 04:02:38 +00:00
$this -> __backAssociation [ $assoc ] = $this -> { $assoc };
}
2006-07-30 12:24:03 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $models as $model ) {
2006-07-30 12:24:03 +00:00
$this -> __backAssociation = array_merge ( $this -> __backAssociation , $this -> { $assoc });
2007-10-22 02:11:58 +00:00
unset ( $this -> __backAssociation [ $model ]);
2006-07-30 12:24:03 +00:00
unset ( $this -> { $assoc }[ $model ]);
}
}
return true ;
}
/**
2007-11-05 03:36:12 +00:00
* Create a set of associations
2006-07-30 12:24:03 +00:00
*
* @ access private
*/
function __createLinks () {
2007-06-20 06:15:35 +00:00
foreach ( $this -> __associations as $type ) {
2006-07-30 12:24:03 +00:00
if ( ! is_array ( $this -> { $type })) {
$this -> { $type } = explode ( ',' , $this -> { $type });
2007-06-20 06:15:35 +00:00
foreach ( $this -> { $type } as $i => $className ) {
2006-07-30 12:24:03 +00:00
$className = trim ( $className );
unset ( $this -> { $type }[ $i ]);
$this -> { $type }[ $className ] = array ();
}
}
2007-06-20 06:15:35 +00:00
foreach ( $this -> { $type } as $assoc => $value ) {
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
$plugin = null ;
2006-07-30 12:24:03 +00:00
if ( is_numeric ( $assoc )) {
unset ( $this -> { $type }[ $assoc ]);
$assoc = $value ;
$value = array ();
$this -> { $type }[ $assoc ] = $value ;
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
if ( strpos ( $assoc , '.' ) !== false ) {
$value = $this -> { $type }[ $assoc ];
unset ( $this -> { $type }[ $assoc ]);
list ( $plugin , $assoc ) = explode ( '.' , $assoc );
$this -> { $type }[ $assoc ] = $value ;
$plugin = $plugin . '.' ;
}
2006-07-30 12:24:03 +00:00
}
2008-01-16 07:34:42 +00:00
$className = $assoc ;
2006-07-30 12:24:03 +00:00
if ( isset ( $value [ 'className' ]) && ! empty ( $value [ 'className' ])) {
$className = $value [ 'className' ];
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
if ( strpos ( $className , '.' ) !== false ) {
list ( $plugin , $className ) = explode ( '.' , $className );
$plugin = $plugin . '.' ;
$this -> { $type }[ $assoc ][ 'className' ] = $className ;
}
2006-07-30 12:24:03 +00:00
}
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
$this -> __constructLinkedModel ( $assoc , $plugin . $className );
2007-01-28 01:53:35 +00:00
}
2006-07-30 12:24:03 +00:00
$this -> __generateAssociation ( $type );
}
}
/**
* Private helper method to create associated models of given class .
2007-11-05 03:36:12 +00:00
*
* @ param string $assoc Association name
2006-07-30 12:24:03 +00:00
* @ param string $className Class name
2007-08-29 20:47:03 +00:00
* @ deprecated $this -> $className use $this -> $assoc instead . $assoc is the 'key' in the associations array ;
* examples : var $hasMany = array ( 'Assoc' => array ( 'className' => 'ModelName' ));
* usage : $this -> Assoc -> modelMethods ();
*
* var $hasMany = array ( 'ModelName' );
* usage : $this -> ModelName -> modelMethods ();
2006-07-30 12:24:03 +00:00
* @ access private
*/
2007-10-22 02:11:58 +00:00
function __constructLinkedModel ( $assoc , $className = null ) {
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
if ( empty ( $className )) {
2007-08-14 23:12:24 +00:00
$className = $assoc ;
}
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
$model = array ( 'class' => $className , 'alias' => $assoc );
2006-11-22 20:08:53 +00:00
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
if ( PHP5 ) {
$this -> { $assoc } = ClassRegistry :: init ( $model );
2006-07-30 12:24:03 +00:00
} else {
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
$this -> { $assoc } =& ClassRegistry :: init ( $model );
}
if ( $assoc ) {
$this -> tableToModel [ $this -> { $assoc } -> table ] = $assoc ;
2006-07-30 12:24:03 +00:00
}
}
/**
* Build array - based association from string .
*
2007-01-31 23:25:05 +00:00
* @ param string $type 'belongsTo' , 'hasOne' , 'hasMany' , 'hasAndBelongsToMany'
2006-07-30 12:24:03 +00:00
* @ access private
*/
function __generateAssociation ( $type ) {
2007-06-20 06:15:35 +00:00
foreach ( $this -> { $type } as $assocKey => $assocData ) {
2006-07-30 12:24:03 +00:00
$class = $assocKey ;
2007-06-20 06:15:35 +00:00
foreach ( $this -> __associationKeys [ $type ] as $key ) {
2008-01-08 06:29:51 +00:00
if ( ! isset ( $this -> { $type }[ $assocKey ][ $key ]) || $this -> { $type }[ $assocKey ][ $key ] === null ) {
2006-07-30 12:24:03 +00:00
$data = '' ;
switch ( $key ) {
case 'fields' :
$data = '' ;
break ;
case 'foreignKey' :
2007-01-31 23:25:05 +00:00
$data = ife ( $type == 'belongsTo' , Inflector :: underscore ( $assocKey ) . '_id' , Inflector :: singularize ( $this -> table ) . '_id' );
2006-07-30 12:24:03 +00:00
break ;
case 'associationForeignKey' :
$data = Inflector :: singularize ( $this -> { $class } -> table ) . '_id' ;
break ;
2007-08-29 10:14:14 +00:00
case 'with' :
$data = Inflector :: camelize ( Inflector :: singularize ( $this -> { $type }[ $assocKey ][ 'joinTable' ]));
break ;
2006-07-30 12:24:03 +00:00
case 'joinTable' :
$tables = array ( $this -> table , $this -> { $class } -> table );
sort ( $tables );
$data = $tables [ 0 ] . '_' . $tables [ 1 ];
break ;
case 'className' :
$data = $class ;
break ;
2007-08-29 10:14:14 +00:00
2008-01-01 23:57:17 +00:00
case 'unique' :
$data = true ;
break ;
2006-07-30 12:24:03 +00:00
}
$this -> { $type }[ $assocKey ][ $key ] = $data ;
}
}
2007-08-14 23:12:24 +00:00
2007-08-29 10:14:14 +00:00
if ( isset ( $this -> { $type }[ $assocKey ][ 'with' ]) && ! empty ( $this -> { $type }[ $assocKey ][ 'with' ])) {
$joinClass = $this -> { $type }[ $assocKey ][ 'with' ];
2007-12-23 17:03:19 +00:00
if ( is_array ( $joinClass )) {
$joinClass = key ( $joinClass );
}
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
$plugin = null ;
if ( strpos ( $joinClass , '.' ) !== false ) {
list ( $plugin , $joinClass ) = explode ( '.' , $joinClass );
$plugin = $plugin . '.' ;
$this -> { $type }[ $assocKey ][ 'with' ] = $joinClass ;
}
if ( ! App :: import ( 'Model' , $plugin . $joinClass )) {
2007-08-29 20:47:03 +00:00
$this -> { $joinClass } = new AppModel ( array (
'name' => $joinClass ,
'table' => $this -> { $type }[ $assocKey ][ 'joinTable' ],
'ds' => $this -> useDbConfig
));
2007-08-29 10:14:14 +00:00
$this -> { $joinClass } -> primaryKey = $this -> { $type }[ $assocKey ][ 'foreignKey' ];
} else {
2008-03-01 03:25:46 +00:00
$this -> __constructLinkedModel ( $joinClass , $plugin . $joinClass );
2007-09-03 04:48:58 +00:00
$this -> { $joinClass } -> primaryKey = $this -> { $type }[ $assocKey ][ 'foreignKey' ];
2007-08-29 10:14:14 +00:00
$this -> { $type }[ $assocKey ][ 'joinTable' ] = $this -> { $joinClass } -> table ;
2007-08-21 21:46:59 +00:00
}
2007-09-03 04:48:58 +00:00
2007-10-28 04:18:18 +00:00
if ( count ( $this -> { $joinClass } -> _schema ) > 2 ) {
if ( isset ( $this -> { $joinClass } -> _schema [ 'id' ])) {
2007-09-03 04:48:58 +00:00
$this -> { $joinClass } -> primaryKey = 'id' ;
}
}
2007-01-31 23:25:05 +00:00
}
2006-07-30 12:24:03 +00:00
}
}
/**
* Sets a custom table for your controller class . Used by your controller to select a database table .
*
* @ param string $tableName Name of the custom table
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function setSource ( $tableName ) {
2007-01-10 11:15:43 +00:00
$this -> setDataSource ( $this -> useDbConfig );
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2007-04-08 23:30:31 +00:00
$db -> cacheSources = $this -> cacheSources ;
2007-08-29 00:16:13 +00:00
2006-07-30 12:24:03 +00:00
if ( $db -> isInterfaceSupported ( 'listSources' )) {
$sources = $db -> listSources ();
2007-12-08 06:08:03 +00:00
if ( is_array ( $sources ) && ! in_array ( strtolower ( $this -> tablePrefix . $tableName ), array_map ( 'strtolower' , $sources ))) {
2006-07-30 12:24:03 +00:00
return $this -> cakeError ( 'missingTable' , array ( array (
2007-10-29 00:17:37 +00:00
'className' => $this -> alias ,
'table' => $this -> tablePrefix . $tableName
)));
2006-07-30 12:24:03 +00:00
}
2007-10-06 05:31:46 +00:00
$this -> _schema = null ;
2006-07-30 12:24:03 +00:00
}
2007-08-29 00:16:13 +00:00
$this -> table = $this -> useTable = $tableName ;
2007-10-27 05:29:11 +00:00
$this -> tableToModel [ $this -> table ] = $this -> alias ;
2007-10-28 04:18:18 +00:00
$this -> schema ();
2006-07-30 12:24:03 +00:00
}
/**
* This function does two things : 1 ) it scans the array $one for the primary key ,
* and if that ' s found , it sets the current id to the value of $one [ id ] .
* For all other keys than 'id' the keys and values of $one are copied to the 'data' property of this object .
* 2 ) Returns an array with all of $one ' s keys and values .
* ( Alternative indata : two strings , which are mangled to
* a one - item , two - dimensional array using $one for a key and $two as its value . )
*
* @ param mixed $one Array or string of data
* @ param string $two Value string for the alternative indata method
2007-11-05 03:36:12 +00:00
* @ return array Data with all of $one ' s keys and values
* @ access public
2006-07-30 12:24:03 +00:00
*/
function set ( $one , $two = null ) {
2007-12-02 22:23:29 +00:00
if ( ! $one ) {
return ;
}
2006-10-09 22:00:22 +00:00
if ( is_object ( $one )) {
2007-06-30 06:52:39 +00:00
$one = Set :: reverse ( $one );
2006-10-09 22:00:22 +00:00
}
2006-07-30 12:24:03 +00:00
if ( is_array ( $one )) {
2006-11-08 03:28:24 +00:00
if ( Set :: countDim ( $one ) == 1 ) {
2007-10-27 01:32:17 +00:00
$data = array ( $this -> alias => $one );
2006-07-30 12:24:03 +00:00
} else {
$data = $one ;
}
} else {
2007-10-27 01:32:17 +00:00
$data = array ( $this -> alias => array ( $one => $two ));
2006-07-30 12:24:03 +00:00
}
2007-06-20 06:15:35 +00:00
foreach ( $data as $n => $v ) {
2006-07-30 12:24:03 +00:00
if ( is_array ( $v )) {
2007-10-17 21:19:30 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $v as $x => $y ) {
2007-12-06 15:21:05 +00:00
if ( isset ( $this -> validationErrors [ $x ])) {
unset ( $this -> validationErrors [ $x ]);
}
2007-10-19 03:33:19 +00:00
2007-12-06 15:21:05 +00:00
if ( $n === $this -> alias ) {
if ( $x === $this -> primaryKey ) {
$this -> id = $y ;
2006-07-30 12:24:03 +00:00
}
}
2007-12-06 15:21:05 +00:00
if ( is_array ( $y ) || is_object ( $y )) {
$y = $this -> deconstruct ( $x , $y );
}
$this -> data [ $n ][ $x ] = $y ;
2006-07-30 12:24:03 +00:00
}
}
}
return $data ;
}
2007-11-08 00:05:12 +00:00
/**
* Deconstructs a complex data type ( array or object ) into a single field value
*
* @ param string $field The name of the field to be deconstructed
* @ param mixed $data An array or object to be deconstructed into a field
* @ return mixed The resulting data that should be assigned to a field
* @ access public
*/
function deconstruct ( $field , $data ) {
$copy = $data ;
$type = $this -> getColumnType ( $field );
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
if ( in_array ( $type , array ( 'datetime' , 'timestamp' , 'date' , 'time' ))) {
$useNewDate = ( isset ( $data [ 'year' ]) || isset ( $data [ 'month' ]) || isset ( $data [ 'day' ]) || isset ( $data [ 'hour' ]) || isset ( $data [ 'minute' ]));
$dateFields = array ( 'Y' => 'year' , 'm' => 'month' , 'd' => 'day' , 'H' => 'hour' , 'i' => 'min' , 's' => 'sec' );
$format = $db -> columns [ $type ][ 'format' ];
$date = array ();
2007-11-26 18:00:06 +00:00
if ( isset ( $data [ 'hour' ]) && isset ( $data [ 'meridian' ]) && $data [ 'hour' ] != 12 && 'pm' == $data [ 'meridian' ]) {
2007-11-08 00:05:12 +00:00
$data [ 'hour' ] = $data [ 'hour' ] + 12 ;
}
2007-11-26 18:00:06 +00:00
if ( isset ( $data [ 'hour' ]) && isset ( $data [ 'meridian' ]) && $data [ 'hour' ] == 12 && 'am' == $data [ 'meridian' ]) {
2007-11-08 00:05:12 +00:00
$data [ 'hour' ] = '00' ;
}
foreach ( $dateFields as $key => $val ) {
if ( in_array ( $val , array ( 'hour' , 'min' , 'sec' ))) {
2007-12-14 04:21:58 +00:00
if ( ! isset ( $data [ $val ]) || $data [ $val ] === '0' || empty ( $data [ $val ])) {
2007-11-08 00:05:12 +00:00
$data [ $val ] = '00' ;
2007-12-23 05:40:25 +00:00
} else {
$data [ $val ] = sprintf ( '%02d' , $data [ $val ]);
2007-11-08 00:05:12 +00:00
}
}
2007-12-14 04:21:58 +00:00
if ( in_array ( $type , array ( 'datetime' , 'timestamp' , 'date' )) && ! isset ( $data [ $val ]) || isset ( $data [ $val ]) && ( empty ( $data [ $val ]) || $data [ $val ][ 0 ] === '-' )) {
return null ;
2007-12-23 05:40:25 +00:00
} elseif ( isset ( $data [ $val ]) && ! empty ( $data [ $val ])) {
$date [ $key ] = $data [ $val ];
2007-12-14 04:21:58 +00:00
}
2007-11-08 00:05:12 +00:00
}
$date = str_replace ( array_keys ( $date ), array_values ( $date ), $format );
2007-12-14 04:21:58 +00:00
if ( $useNewDate && ( ! empty ( $date ))) {
2007-11-08 00:05:12 +00:00
return $date ;
}
}
return $data ;
}
2006-07-30 12:24:03 +00:00
/**
* Returns an array of table metadata ( column names and types ) from the database .
2007-08-21 21:46:59 +00:00
* $field => keys ( type , null , default , key , length , extra )
2006-07-30 12:24:03 +00:00
*
2008-01-21 02:21:28 +00:00
* @ param mixed $field Set to true to reload schema , or a string to return a specific field
2006-07-30 12:24:03 +00:00
* @ return array Array of table metadata
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-01-21 02:21:28 +00:00
function schema ( $field = false ) {
if ( ! is_array ( $this -> _schema ) || $field === true ) {
2007-04-18 16:39:11 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
$db -> cacheSources = $this -> cacheSources ;
if ( $db -> isInterfaceSupported ( 'describe' ) && $this -> useTable !== false ) {
2008-01-21 02:21:28 +00:00
$this -> _schema = $db -> describe ( $this , $field );
2007-04-18 16:39:11 +00:00
} elseif ( $this -> useTable === false ) {
2007-10-28 04:18:18 +00:00
$this -> _schema = array ();
2007-04-18 16:39:11 +00:00
}
2006-07-30 12:24:03 +00:00
}
2008-01-21 02:21:28 +00:00
if ( is_string ( $field )) {
if ( isset ( $this -> _schema [ $field ])) {
return $this -> _schema [ $field ];
} else {
return null ;
}
}
2007-08-21 21:46:59 +00:00
return $this -> _schema ;
}
/**
* @ deprecated
2007-11-05 03:36:12 +00:00
* @ see Model :: schema ()
2007-08-21 21:46:59 +00:00
*/
function loadInfo ( $clear = false ) {
2008-03-07 01:29:19 +00:00
trigger_error ( __ ( '(Model::loadInfo) Deprecated - See Model::schema()' , true ), E_USER_WARNING );
2007-10-28 04:18:18 +00:00
$info = $this -> schema ( $clear );
if ( is_array ( $info )) {
2007-10-15 07:48:19 +00:00
$fields = array ();
2007-10-28 04:18:18 +00:00
foreach ( $info as $field => $value ) {
2007-12-08 06:08:03 +00:00
$fields [] = array_merge ( array ( 'name' => $field ), $value );
2007-10-15 07:48:19 +00:00
}
unset ( $info );
2007-10-28 04:18:18 +00:00
return new Set ( $fields );
2007-08-21 21:46:59 +00:00
}
2006-07-30 12:24:03 +00:00
}
/**
* Returns an associative array of field names and column types .
*
2007-11-05 03:36:12 +00:00
* @ return array Field types indexed by field name
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getColumnTypes () {
2007-10-28 04:18:18 +00:00
$columns = $this -> schema ();
if ( empty ( $columns )) {
trigger_error ( __ ( '(Model::getColumnTypes) Unable to build model field data. If you are using a model without a database table, try implementing schema()' , true ), E_USER_WARNING );
2007-05-05 17:36:30 +00:00
}
2007-10-28 04:18:18 +00:00
$cols = array ();
foreach ( $columns as $field => $values ) {
$cols [ $field ] = $values [ 'type' ];
}
return $cols ;
2006-07-30 12:24:03 +00:00
}
/**
* Returns the column type of a column in the model
*
* @ param string $column The name of the model column
* @ return string Column type
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getColumnType ( $column ) {
2007-10-28 04:18:18 +00:00
$cols = $this -> schema ();
2008-02-28 01:53:54 +00:00
if ( isset ( $cols [ $column ]) && isset ( $cols [ $column ][ 'type' ])) {
2007-10-28 04:18:18 +00:00
return $cols [ $column ][ 'type' ];
2006-07-30 12:24:03 +00:00
}
2008-02-28 01:53:54 +00:00
return 'string' ;
2006-07-30 12:24:03 +00:00
}
/**
* Returns true if this Model has given field in its database table .
*
* @ param string $name Name of field to look for
2007-11-05 03:36:12 +00:00
* @ return bool Success
* @ access public
2006-07-30 12:24:03 +00:00
*/
function hasField ( $name ) {
2006-12-19 19:26:02 +00:00
if ( is_array ( $name )) {
foreach ( $name as $n ) {
if ( $this -> hasField ( $n )) {
return $n ;
}
}
return false ;
}
2007-10-28 04:18:18 +00:00
if ( empty ( $this -> _schema )) {
$this -> schema ();
2006-07-30 12:24:03 +00:00
}
2007-10-28 04:18:18 +00:00
if ( $this -> _schema != null ) {
return isset ( $this -> _schema [ $name ]);
2006-07-30 12:24:03 +00:00
}
2006-11-21 22:40:30 +00:00
return false ;
2006-07-30 12:24:03 +00:00
}
/**
2007-11-05 03:36:12 +00:00
* Initializes the model for writing a new record , loading the default values
* for those fields that are not defined in $data .
2006-07-30 12:24:03 +00:00
*
2008-01-10 08:44:38 +00:00
* @ param mixed $data Optional data array to assign to the model after it is created . If null or false ,
* schema data defaults are not merged .
2008-02-06 02:35:12 +00:00
* @ param boolean $filterKey If true , overwrites any primary key input with an empty value
2008-04-03 15:22:18 +00:00
* @ return array The current Model :: data ; after merging $data and / or defaults from database
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-02-06 02:35:12 +00:00
function create ( $data = array (), $filterKey = false ) {
2008-01-10 08:44:38 +00:00
$defaults = array ();
2006-07-30 12:24:03 +00:00
$this -> id = false ;
$this -> data = array ();
2007-01-22 08:22:10 +00:00
$this -> validationErrors = array ();
2007-12-19 02:05:15 +00:00
if ( $data !== null && $data !== false ) {
2008-01-10 08:44:38 +00:00
foreach ( $this -> schema () as $field => $properties ) {
if ( $this -> primaryKey !== $field && isset ( $properties [ 'default' ])) {
$defaults [ $field ] = $properties [ 'default' ];
}
}
2007-12-19 02:05:15 +00:00
$this -> set ( Set :: filter ( $defaults ));
$this -> set ( $data );
}
2008-02-06 02:35:12 +00:00
if ( $filterKey ) {
$this -> set ( $this -> primaryKey , false );
}
2006-10-27 18:14:35 +00:00
return $this -> data ;
2006-07-30 12:24:03 +00:00
}
/**
2007-11-05 03:36:12 +00:00
* Returns a list of fields from the database , and sets the current model
* data ( Model :: $data ) with the record found .
2006-07-30 12:24:03 +00:00
*
* @ param mixed $fields String of single fieldname , or an array of fieldnames .
2007-11-05 03:36:12 +00:00
* @ param mixed $id The ID of the record to read
* @ return array Array of database fields , or false if not found
* @ access public
2006-07-30 12:24:03 +00:00
*/
function read ( $fields = null , $id = null ) {
$this -> validationErrors = array ();
if ( $id != null ) {
$this -> id = $id ;
}
$id = $this -> id ;
if ( is_array ( $this -> id )) {
$id = $this -> id [ 0 ];
}
2008-01-01 22:28:47 +00:00
if ( $id !== null && $id !== false ) {
2007-10-27 01:32:17 +00:00
$this -> data = $this -> find ( array ( $this -> alias . '.' . $this -> primaryKey => $id ), $fields );
2007-02-08 17:50:47 +00:00
return $this -> data ;
2006-07-30 12:24:03 +00:00
} else {
return false ;
}
}
/**
* Returns contents of a field in a query matching given conditions .
*
* @ param string $name Name of field to get
* @ param array $conditions SQL conditions ( defaults to NULL )
* @ param string $order SQL ORDER BY fragment
2007-11-05 03:36:12 +00:00
* @ return string field contents , or false if not found
* @ access public
2006-07-30 12:24:03 +00:00
*/
function field ( $name , $conditions = null , $order = null ) {
Fixes #2902, DB_ACL::allow allowing all when $actions is not an array.
Fixes #2988, AclComponent check() does not inherit permissions.
Fixes #3022, Inconsistent table alias quoting crashes Acl node lookup with PostgreSQL.
Fixes #3129, Console ACL Shell ACO View Broken
Fixes #3176, Problems with ACL support on Microsoft SQL Server.
Closes #3311 as invalid, DboSourceTest::testArrayConditionsParsing tests added
Fixes #3312, DB_ACL::check() fail returning right permission
Fixes #3344, Model->field adds incorrect condition under certain circumstances.
Fixes #3400, Cookie Component: When reading a non-existing key it throws a notice.
Fixes #3407, Since [5768] CookieComponent throws warning when used in beforeFilter().
Closes #3401, Added form test to ensure $Form->fields array is what the security component requires.
Updated AclComponentTest
Merged changes in app/ to cake/console/libs/templates/skel
Fixed generated link to Run More Test after running Group > All tests
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5776 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-10-17 12:51:17 +00:00
if ( $conditions === null && $this -> id !== false ) {
2007-10-27 01:32:17 +00:00
$conditions = array ( $this -> alias . '.' . $this -> primaryKey => $this -> id );
2006-07-30 12:24:03 +00:00
}
2007-06-20 06:15:35 +00:00
if ( $this -> recursive >= 1 ) {
2007-01-10 11:15:43 +00:00
$recursive = - 1 ;
} else {
$recursive = $this -> recursive ;
}
if ( $data = $this -> find ( $conditions , $name , $order , $recursive )) {
2006-07-30 12:24:03 +00:00
if ( strpos ( $name , '.' ) === false ) {
2007-10-27 01:32:17 +00:00
if ( isset ( $data [ $this -> alias ][ $name ])) {
return $data [ $this -> alias ][ $name ];
2006-07-30 12:24:03 +00:00
}
} else {
$name = explode ( '.' , $name );
if ( isset ( $data [ $name [ 0 ]][ $name [ 1 ]])) {
return $data [ $name [ 0 ]][ $name [ 1 ]];
}
}
2007-03-26 18:38:54 +00:00
if ( isset ( $data [ 0 ]) && count ( $data [ 0 ]) > 0 ) {
$name = key ( $data [ 0 ]);
return $data [ 0 ][ $name ];
}
2006-07-30 12:24:03 +00:00
} else {
return false ;
}
}
/**
* Saves a single field to the database .
*
* @ param string $name Name of the table field
* @ param mixed $value Value of the field
2007-10-22 16:54:36 +00:00
* @ param boolean $validate Whether or not this model should validate before saving ( defaults to false )
2007-10-22 16:09:35 +00:00
* @ return boolean True on success save
2007-11-05 03:36:12 +00:00
* @ access public
* @ see Model :: save ()
2006-07-30 12:24:03 +00:00
*/
function saveField ( $name , $value , $validate = false ) {
2008-05-15 03:16:36 +00:00
$id = $this -> id ;
$this -> create ( false );
return $this -> save ( array ( $this -> alias => array (
$this -> primaryKey => $id ,
$name => $value ,
)), $validate , array ( $name ));
2006-07-30 12:24:03 +00:00
}
/**
2007-11-05 03:36:12 +00:00
* Saves model data to the database . By default , validation occurs before save .
2006-07-30 12:24:03 +00:00
*
* @ param array $data Data to save .
2007-10-22 16:54:36 +00:00
* @ param boolean $validate If set , validation will be done before the save
2006-07-30 12:24:03 +00:00
* @ param array $fieldList List of fields to allow to be written
2007-11-05 03:36:12 +00:00
* @ return mixed On success Model :: $data if its not empty or true , false on failure
* @ access public
2006-07-30 12:24:03 +00:00
*/
function save ( $data = null , $validate = true , $fieldList = array ()) {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2007-10-17 21:19:30 +00:00
$_whitelist = $this -> whitelist ;
2008-03-07 01:29:19 +00:00
$defaults = array ( 'validate' => true , 'fieldList' => array (), 'callbacks' => true );
2007-12-03 16:04:29 +00:00
$fields = array ();
2007-10-17 21:19:30 +00:00
2008-03-07 01:29:19 +00:00
if ( ! is_array ( $validate )) {
$options = array_merge ( $defaults , compact ( 'validate' , 'fieldList' , 'callbacks' ));
} else {
$options = array_merge ( $defaults , $validate );
}
if ( ! empty ( $options [ 'fieldList' ])) {
$this -> whitelist = $options [ 'fieldList' ];
} elseif ( $options [ 'fieldList' ] === null ) {
2007-10-17 21:19:30 +00:00
$this -> whitelist = array ();
}
2006-10-09 22:00:22 +00:00
$this -> set ( $data );
2007-10-19 21:05:34 +00:00
2007-12-19 02:05:15 +00:00
if ( empty ( $this -> data ) && ! $this -> hasField ( array ( 'created' , 'updated' , 'modified' ))) {
2007-10-19 21:05:34 +00:00
return false ;
}
2007-10-17 21:19:30 +00:00
foreach ( array ( 'created' , 'updated' , 'modified' ) as $field ) {
2007-12-03 16:04:29 +00:00
if ( isset ( $this -> data [ $this -> alias ]) && array_key_exists ( $field , $this -> data [ $this -> alias ]) && $this -> data [ $this -> alias ][ $field ] === null ) {
2007-10-27 01:32:17 +00:00
unset ( $this -> data [ $this -> alias ][ $field ]);
2007-10-17 21:19:30 +00:00
}
}
$exists = $this -> exists ();
2007-12-06 15:21:05 +00:00
$dateFields = array ( 'modified' , 'updated' );
2008-03-07 01:29:19 +00:00
2007-12-06 15:21:05 +00:00
if ( ! $exists ) {
$dateFields [] = 'created' ;
2007-12-03 16:04:29 +00:00
}
2007-12-06 15:21:05 +00:00
if ( isset ( $this -> data [ $this -> alias ])) {
$fields = array_keys ( $this -> data [ $this -> alias ]);
2007-10-17 21:19:30 +00:00
}
2008-05-13 23:34:59 +00:00
if ( $options [ 'validate' ] && ! $this -> validates ( $options )) {
2008-01-01 19:34:40 +00:00
$this -> whitelist = $_whitelist ;
return false ;
}
2007-12-06 15:21:05 +00:00
foreach ( $dateFields as $updateCol ) {
2007-10-17 21:19:30 +00:00
if ( $this -> hasField ( $updateCol ) && ! in_array ( $updateCol , $fields )) {
2007-12-08 06:08:03 +00:00
$colType = array_merge ( array ( 'formatter' => 'date' ), $db -> columns [ $this -> getColumnType ( $updateCol )]);
2007-12-06 15:21:05 +00:00
if ( ! array_key_exists ( 'formatter' , $colType ) || ! array_key_exists ( 'format' , $colType )) {
$time = strtotime ( 'now' );
} else {
$time = $colType [ 'formatter' ]( $colType [ 'format' ]);
}
2008-01-01 19:34:40 +00:00
if ( ! empty ( $this -> whitelist )) {
$this -> whitelist [] = $updateCol ;
}
2007-12-06 15:21:05 +00:00
$this -> set ( $updateCol , $time );
2007-10-17 21:19:30 +00:00
}
}
2006-07-30 12:24:03 +00:00
2008-03-07 01:29:19 +00:00
if ( $options [ 'callbacks' ] === true || $options [ 'callbacks' ] == 'before' ) {
2008-05-13 23:34:59 +00:00
if ( ! $this -> Behaviors -> trigger ( $this , 'beforeSave' , array ( $options ), array ( 'break' => true , 'breakOn' => false )) || ! $this -> beforeSave ( $options )) {
2008-03-07 01:29:19 +00:00
$this -> whitelist = $_whitelist ;
return false ;
}
2006-07-30 12:24:03 +00:00
}
$fields = $values = array ();
2007-12-03 16:04:29 +00:00
2008-01-09 04:50:08 +00:00
if ( isset ( $this -> data [ $this -> alias ][ $this -> primaryKey ]) && empty ( $this -> data [ $this -> alias ][ $this -> primaryKey ])) {
unset ( $this -> data [ $this -> alias ][ $this -> primaryKey ]);
}
2007-06-20 06:15:35 +00:00
foreach ( $this -> data as $n => $v ) {
2008-01-01 23:57:17 +00:00
if ( isset ( $this -> hasAndBelongsToMany [ $n ])) {
if ( isset ( $v [ $n ])) {
$v = $v [ $n ];
}
$joined [ $n ] = $v ;
2006-07-30 12:24:03 +00:00
} else {
2007-10-27 01:32:17 +00:00
if ( $n === $this -> alias ) {
2006-09-11 00:35:06 +00:00
foreach ( array ( 'created' , 'updated' , 'modified' ) as $field ) {
2007-10-17 21:19:30 +00:00
if ( array_key_exists ( $field , $v ) && empty ( $v [ $field ])) {
2006-09-11 00:35:06 +00:00
unset ( $v [ $field ]);
}
}
2006-09-14 18:50:33 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $v as $x => $y ) {
2007-12-06 15:21:05 +00:00
if ( $this -> hasField ( $x ) && ( empty ( $this -> whitelist ) || in_array ( $x , $this -> whitelist ))) {
2008-01-01 23:57:17 +00:00
list ( $fields [], $values []) = array ( $x , $y );
2006-07-30 12:24:03 +00:00
}
}
}
}
}
2007-06-28 02:47:16 +00:00
$count = count ( $fields );
2006-07-30 12:24:03 +00:00
2007-06-28 02:47:16 +00:00
if ( ! $exists && $count > 0 ) {
2006-07-30 12:24:03 +00:00
$this -> id = false ;
}
2007-06-28 02:47:16 +00:00
$success = true ;
2007-02-07 00:36:19 +00:00
$created = false ;
2006-07-30 12:24:03 +00:00
2007-06-28 02:47:16 +00:00
if ( $count > 0 ) {
2006-07-30 12:24:03 +00:00
if ( ! empty ( $this -> id )) {
2007-06-28 02:47:16 +00:00
if ( ! $db -> update ( $this , $fields , $values )) {
$success = false ;
2006-07-30 12:24:03 +00:00
}
} else {
2007-10-28 04:18:18 +00:00
foreach ( $this -> _schema as $field => $properties ) {
if ( $this -> primaryKey === $field ) {
2008-05-19 05:08:37 +00:00
$isUUID = ( $this -> _schema [ $field ][ 'type' ] === 'string' && $this -> _schema [ $field ][ 'length' ] === 36 )
|| ( $this -> _schema [ $field ][ 'type' ] === 'binary' && $this -> _schema [ $field ][ 'length' ] === 16 );
if ( empty ( $this -> data [ $this -> alias ][ $this -> primaryKey ]) && $isUUID ) {
2008-01-01 23:57:17 +00:00
list ( $fields [], $values []) = array ( $this -> primaryKey , String :: uuid ());
2007-08-20 01:58:44 +00:00
}
break ;
}
}
2007-06-28 02:47:16 +00:00
if ( ! $db -> create ( $this , $fields , $values )) {
$success = $created = false ;
} else {
$created = true ;
2007-02-07 00:36:19 +00:00
}
}
2007-06-28 02:47:16 +00:00
}
2006-07-30 12:24:03 +00:00
2008-01-08 06:29:51 +00:00
if ( ! empty ( $this -> belongsTo )) {
$this -> updateCounterCache ( array (), $created );
}
2007-06-28 02:47:16 +00:00
if ( ! empty ( $joined ) && $success === true ) {
$this -> __saveMulti ( $joined , $this -> id );
}
if ( $success && $count > 0 ) {
2007-11-28 16:44:59 +00:00
if ( ! empty ( $this -> data )) {
$success = $this -> data ;
}
2008-03-07 01:29:19 +00:00
if ( $options [ 'callbacks' ] === true || $options [ 'callbacks' ] == 'after' ) {
2008-05-13 23:34:59 +00:00
$this -> Behaviors -> trigger ( $this , 'afterSave' , array ( $created , $options ));
2008-03-07 01:29:19 +00:00
$this -> afterSave ( $created );
}
2007-10-28 04:18:18 +00:00
if ( ! empty ( $this -> data )) {
2008-05-15 15:37:55 +00:00
$success = Set :: merge ( $success , $this -> data );
2007-10-24 18:53:25 +00:00
}
2007-06-28 02:47:16 +00:00
$this -> data = false ;
2007-10-08 17:43:14 +00:00
$this -> __exists = null ;
2007-06-28 02:47:16 +00:00
$this -> _clearCache ();
$this -> validationErrors = array ();
2006-07-30 12:24:03 +00:00
}
2007-10-17 21:19:30 +00:00
$this -> whitelist = $_whitelist ;
2007-02-07 00:36:19 +00:00
return $success ;
2006-07-30 12:24:03 +00:00
}
/**
* Saves model hasAndBelongsToMany data to the database .
*
2007-11-05 03:36:12 +00:00
* @ param array $joined Data to save
* @ param mixed $id ID of record in this model
2006-07-30 12:24:03 +00:00
* @ access private
*/
function __saveMulti ( $joined , $id ) {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-01-01 23:57:17 +00:00
foreach ( $joined as $assoc => $value ) {
2008-01-03 18:50:59 +00:00
$newValues = array ();
2008-03-10 00:25:59 +00:00
if ( empty ( $value )) {
$value = array ();
}
2008-01-01 23:57:17 +00:00
if ( isset ( $this -> hasAndBelongsToMany [ $assoc ])) {
list ( $join ) = $this -> joinModel ( $this -> hasAndBelongsToMany [ $assoc ][ 'with' ]);
2008-02-29 22:01:09 +00:00
$conditions = array ( $join . '.' . $this -> hasAndBelongsToMany [ $assoc ][ 'foreignKey' ] => $id );
2008-01-01 23:57:17 +00:00
$links = array ();
if ( $this -> hasAndBelongsToMany [ $assoc ][ 'unique' ]) {
$this -> { $join } -> deleteAll ( $conditions );
} else {
list ( $recursive , $fields ) = array ( - 1 , $this -> hasAndBelongsToMany [ $assoc ][ 'associationForeignKey' ]);
$links = Set :: extract (
$this -> { $join } -> find ( 'all' , compact ( 'conditions' , 'recursive' , 'fields' )),
" { n}. { $join } . " . $this -> hasAndBelongsToMany [ $assoc ][ 'associationForeignKey' ]
);
}
foreach ( $value as $update ) {
if ( ! empty ( $update )) {
if ( is_array ( $update )) {
$update [ $this -> hasAndBelongsToMany [ $assoc ][ 'foreignKey' ]] = $id ;
$this -> { $join } -> create ( $update );
$this -> { $join } -> save ();
} elseif ( ! in_array ( $update , $links )) {
$values = join ( ',' , array (
$db -> value ( $id , $this -> getColumnType ( $this -> primaryKey )),
$db -> value ( $update )
));
2007-03-31 20:27:47 +00:00
$newValues [] = " ( { $values } ) " ;
2008-01-01 23:57:17 +00:00
unset ( $values );
2007-03-31 20:27:47 +00:00
}
}
2006-07-30 12:24:03 +00:00
}
2007-04-08 23:30:31 +00:00
2008-01-01 23:57:17 +00:00
if ( ! empty ( $newValues )) {
$fields = join ( ',' , array (
$db -> name ( $this -> hasAndBelongsToMany [ $assoc ][ 'foreignKey' ]),
$db -> name ( $this -> hasAndBelongsToMany [ $assoc ][ 'associationForeignKey' ])
));
$db -> insertMulti ( $this -> { $join }, $fields , $newValues );
2006-07-30 12:24:03 +00:00
}
}
}
}
2008-01-01 19:34:40 +00:00
/**
* Updates the counter cache of belongsTo associations after a save or delete operation
*
* @ param array $keys Optional foreign key data , defaults to the information $this -> data
2008-01-10 08:44:38 +00:00
* @ param boolean $created True if a new record was created , otherwise only associations with
* 'counterScope' defined get updated
2008-01-01 19:34:40 +00:00
* @ return void
* @ access public
*/
2008-01-10 08:44:38 +00:00
function updateCounterCache ( $keys = array (), $created = false ) {
2008-01-01 19:34:40 +00:00
if ( empty ( $keys )) {
$keys = $this -> data [ $this -> alias ];
}
foreach ( $this -> belongsTo as $parent => $assoc ) {
if ( ! empty ( $assoc [ 'counterCache' ])) {
if ( $assoc [ 'counterCache' ] === true ) {
$assoc [ 'counterCache' ] = Inflector :: underscore ( $this -> alias ) . '_count' ;
}
2008-02-02 04:50:44 +00:00
if ( ! isset ( $keys [ $assoc [ 'foreignKey' ]]) || empty ( $keys [ $assoc [ 'foreignKey' ]])) {
$keys [ $assoc [ 'foreignKey' ]] = $this -> field ( $assoc [ 'foreignKey' ]);
}
2008-01-01 19:34:40 +00:00
if ( $this -> { $parent } -> hasField ( $assoc [ 'counterCache' ])) {
$conditions = array ( $this -> escapeField ( $assoc [ 'foreignKey' ]) => $keys [ $assoc [ 'foreignKey' ]]);
if ( isset ( $assoc [ 'counterScope' ])) {
$conditions = array_merge ( $conditions , ( array ) $assoc [ 'counterScope' ]);
}
$this -> { $parent } -> updateAll (
array ( $assoc [ 'counterCache' ] => intval ( $this -> find ( 'count' , compact ( 'conditions' )))),
array ( $this -> { $parent } -> escapeField () => $keys [ $assoc [ 'foreignKey' ]])
);
}
}
}
}
/**
* Saves ( a ) multiple individual records for a single model or ( b ) this record , as well as
* all associated records
*
2008-03-07 01:29:19 +00:00
* @ param array $data Record data to save . This can be either a numerically - indexed array ( for saving multiple
* records of the same type ), or an array indexed by association name .
* @ param array $options Options to use when saving record data , which are as follows :
* - validate : Set to false to disable validation , true to validate each record before
* saving , 'first' to validate * all * records before any are saved , or 'only' to only
* validate the records , but not save them .
* - atomic : If true ( default ), will attempt to save all records in a single transaction .
2008-04-18 13:14:40 +00:00
* Should be set to false if database / table does not support transactions .
* If false , we return an array similar to the $data array passed , but values are set to true / false
* depending on whether each record saved successfully .
2008-03-07 01:29:19 +00:00
* - fieldList : Equivalent to the $fieldList parameter in Model :: save ()
* @ return mixed True on success , or false on failure
2008-01-01 19:34:40 +00:00
* @ access public
*/
function saveAll ( $data = null , $options = array ()) {
if ( empty ( $data )) {
2008-02-06 02:35:12 +00:00
$data = $this -> data ;
2008-01-01 19:34:40 +00:00
}
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-03-07 01:29:19 +00:00
$options = array_merge ( array ( 'validate' => true , 'atomic' => true ), $options );
$this -> validationErrors = $validationErrors = array ();
2008-02-21 15:36:13 +00:00
$validates = true ;
2008-02-06 02:35:12 +00:00
$return = array ();
2008-01-01 19:34:40 +00:00
2008-03-07 01:29:19 +00:00
if ( $options [ 'atomic' ] && $options [ 'validate' ] !== 'only' ) {
2008-01-01 19:34:40 +00:00
$db -> begin ( $this );
}
if ( Set :: numeric ( array_keys ( $data ))) {
2008-03-07 01:29:19 +00:00
while ( $validates ) {
2008-02-06 02:35:12 +00:00
foreach ( $data as $record ) {
2008-03-07 01:29:19 +00:00
if ( ! $validates = $this -> __save ( $this , $record , $options )) {
if ( empty ( $this -> id )) {
$validationErrors [] = $this -> validationErrors ;
} else {
$validationErrors [ $this -> id ] = $this -> validationErrors ;
2008-02-21 15:36:13 +00:00
}
}
2008-05-16 06:03:33 +00:00
$validating = ( $options [ 'validate' ] === 'only' || $options [ 'validate' ] === 'first' );
2008-04-18 13:14:40 +00:00
if ( ! $options [ 'atomic' ]) {
$return [] = $validates ;
2008-05-16 06:03:33 +00:00
} elseif ( ! $validates && ! $validating ) {
2008-04-19 17:33:17 +00:00
break ;
2008-04-18 13:14:40 +00:00
}
2008-02-21 15:36:13 +00:00
}
2008-03-07 01:29:19 +00:00
$this -> validationErrors = $validationErrors ;
switch ( true ) {
case ( $options [ 'validate' ] === 'only' ) :
return $validates ;
break ;
case ( $options [ 'validate' ] === 'first' ) :
$options [ 'validate' ] = true ;
continue ;
break ;
default :
if ( $options [ 'atomic' ]) {
2008-03-13 03:11:29 +00:00
if ( $validates && ( $db -> commit ( $this ) !== false )) {
return true ;
2008-03-07 01:29:19 +00:00
}
2008-03-13 03:11:29 +00:00
$db -> rollback ( $this );
return false ;
2008-02-21 15:36:13 +00:00
}
2008-04-18 13:14:40 +00:00
return $return ;
2008-03-07 01:29:19 +00:00
break ;
2008-02-21 15:36:13 +00:00
}
}
2008-05-13 01:39:06 +00:00
return $return ;
2008-03-07 01:29:19 +00:00
}
$associations = $this -> getAssociated ();
2008-02-21 15:36:13 +00:00
2008-03-07 01:29:19 +00:00
while ( $validates ) {
2008-01-01 19:34:40 +00:00
foreach ( $data as $association => $values ) {
2008-03-07 01:29:19 +00:00
if ( isset ( $associations [ $association ])) {
switch ( $associations [ $association ]) {
case 'belongsTo' :
if ( $this -> __save ( $this -> { $association }, $values , $options )) {
$data [ $this -> alias ][ $this -> belongsTo [ $association ][ 'foreignKey' ]] = $this -> { $association } -> id ;
} else {
$validationErrors [ $association ] = $this -> { $association } -> validationErrors ;
$validates = false ;
}
2008-04-18 13:14:40 +00:00
if ( ! $options [ 'atomic' ]) {
$return [ $association ][] = $validates ;
}
2008-03-07 01:29:19 +00:00
break ;
2008-01-01 19:34:40 +00:00
}
}
}
2008-03-07 01:29:19 +00:00
if ( ! $this -> __save ( $this , $data [ $this -> alias ], $options )) {
$validationErrors [ $this -> alias ] = $this -> validationErrors ;
$validates = false ;
2008-01-01 19:34:40 +00:00
}
2008-04-18 13:14:40 +00:00
if ( ! $options [ 'atomic' ]) {
$return [ $this -> alias ][] = $validates ;
}
2008-05-16 06:03:33 +00:00
$validating = ( $options [ 'validate' ] === 'only' || $options [ 'validate' ] === 'first' );
2008-01-01 19:34:40 +00:00
foreach ( $data as $association => $values ) {
2008-05-16 06:03:33 +00:00
if ( ! $validates && ! $validating ) {
2008-05-13 00:39:29 +00:00
break ;
}
2008-01-01 19:34:40 +00:00
if ( isset ( $associations [ $association ])) {
2008-03-07 01:29:19 +00:00
$type = $associations [ $association ];
switch ( $type ) {
2008-01-27 01:57:39 +00:00
case 'hasOne' :
2008-03-07 01:29:19 +00:00
$values [ $this -> { $type }[ $association ][ 'foreignKey' ]] = $this -> id ;
if ( ! $this -> __save ( $this -> { $association }, $values , $options )) {
$validationErrors [ $association ] = $this -> { $association } -> validationErrors ;
$validates = false ;
2008-01-27 01:57:39 +00:00
}
2008-04-18 13:14:40 +00:00
if ( ! $options [ 'atomic' ]) {
$return [ $association ][] = $validates ;
}
2008-01-27 01:57:39 +00:00
break ;
2008-01-01 19:34:40 +00:00
case 'hasMany' :
2008-03-07 01:29:19 +00:00
foreach ( $values as $i => $value ) {
$values [ $i ][ $this -> { $type }[ $association ][ 'foreignKey' ]] = $this -> id ;
}
2008-05-16 05:31:14 +00:00
$_options = array_merge ( $options , array ( 'atomic' => false ));
if ( $_options [ 'validate' ] === 'first' ) {
$_options [ 'validate' ] = 'only' ;
}
$_return = $this -> { $association } -> saveAll ( $values , $_options );
if ( $_return === false || ( is_array ( $_return ) && in_array ( false , $_return , true ))) {
2008-03-07 01:29:19 +00:00
$validationErrors [ $association ] = $this -> { $association } -> validationErrors ;
$validates = false ;
2008-01-27 01:57:39 +00:00
}
2008-05-16 05:31:14 +00:00
if ( is_array ( $_return )) {
foreach ( $_return as $val ) {
if ( ! isset ( $return [ $association ])) {
$return [ $association ] = array ();
} elseif ( ! is_array ( $return [ $association ])) {
$return [ $association ] = array ( $return [ $association ]);
}
$return [ $association ][] = $val ;
}
} else {
$return [ $association ] = $_return ;
2008-04-18 13:14:40 +00:00
}
2008-01-01 19:34:40 +00:00
break ;
}
}
}
2008-05-19 02:31:26 +00:00
2008-03-07 01:29:19 +00:00
$this -> validationErrors = $validationErrors ;
2008-05-19 02:31:26 +00:00
if ( isset ( $validationErrors [ $this -> alias ])) {
$this -> validationErrors = $validationErrors [ $this -> alias ];
}
2008-01-01 19:34:40 +00:00
2008-03-07 01:29:19 +00:00
switch ( true ) {
case ( $options [ 'validate' ] === 'only' ) :
return $validates ;
break ;
case ( $options [ 'validate' ] === 'first' ) :
$options [ 'validate' ] = true ;
continue ;
break ;
default :
if ( $options [ 'atomic' ]) {
if ( $validates ) {
return ( $db -> commit ( $this ) !== false );
} else {
$db -> rollback ( $this );
}
}
2008-04-18 13:14:40 +00:00
return $return ;
2008-03-07 01:29:19 +00:00
break ;
}
}
}
/**
* Private helper method used by saveAll
*
* @ access private
* @ see Model :: saveAll ()
*/
function __save ( & $model , $data , $options ) {
if ( $options [ 'validate' ] === 'first' || $options [ 'validate' ] === 'only' ) {
2008-05-13 23:34:59 +00:00
if ( ! ( $model -> create ( $data ) && $model -> validates ( $options ))) {
2008-03-07 01:29:19 +00:00
return false ;
}
2008-04-19 13:30:11 +00:00
} elseif ( ! ( $model -> create ( null ) !== null && $model -> save ( $data , $options ))) {
2008-03-07 01:29:19 +00:00
return false ;
2008-01-01 19:34:40 +00:00
}
2008-01-08 10:21:25 +00:00
return true ;
2008-01-01 19:34:40 +00:00
}
2006-10-09 22:00:22 +00:00
/**
* Allows model records to be updated based on a set of conditions
*
2007-11-05 03:36:12 +00:00
* @ param array $fields Set of fields and values , indexed by fields
* @ param mixed $conditions Conditions to match , true for all records
2007-10-22 16:09:35 +00:00
* @ return boolean True on success , false on failure
2007-11-05 03:36:12 +00:00
* @ access public
2006-10-09 22:00:22 +00:00
*/
2007-02-08 18:36:26 +00:00
function updateAll ( $fields , $conditions = true ) {
2006-10-20 17:24:37 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
return $db -> update ( $this , $fields , null , $conditions );
2006-10-09 22:00:22 +00:00
}
2006-07-30 12:24:03 +00:00
/**
* Synonym for del () .
*
2007-11-05 03:36:12 +00:00
* @ param mixed $id ID of record to delete
* @ param boolean $cascade Set to true to delete records that depend on this record
2007-10-22 16:09:35 +00:00
* @ return boolean True on success
2007-11-05 03:36:12 +00:00
* @ access public
* @ see Model :: del ()
2006-07-30 12:24:03 +00:00
*/
function remove ( $id = null , $cascade = true ) {
return $this -> del ( $id , $cascade );
}
/**
* Removes record for given id . If no id is given , the current id is used . Returns true on success .
*
2007-11-05 03:36:12 +00:00
* @ param mixed $id ID of record to delete
* @ param boolean $cascade Set to true to delete records that depend on this record
2007-10-22 16:09:35 +00:00
* @ return boolean True on success
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function del ( $id = null , $cascade = true ) {
2006-12-13 04:04:15 +00:00
if ( ! empty ( $id )) {
2006-07-30 12:24:03 +00:00
$this -> id = $id ;
}
2007-04-30 02:46:51 +00:00
$id = $this -> id ;
2006-07-30 12:24:03 +00:00
2007-11-25 05:14:07 +00:00
if ( $this -> exists () && $this -> beforeDelete ( $cascade )) {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-03-09 06:24:40 +00:00
if ( ! $this -> Behaviors -> trigger ( $this , 'beforeDelete' , array ( $cascade ), array ( 'break' => true , 'breakOn' => false ))) {
2008-02-21 15:36:13 +00:00
return false ;
2006-12-19 19:26:02 +00:00
}
2007-04-30 02:46:51 +00:00
$this -> _deleteDependent ( $id , $cascade );
$this -> _deleteLinks ( $id );
2007-05-01 00:19:42 +00:00
$this -> id = $id ;
2006-12-19 19:26:02 +00:00
2008-01-01 19:34:40 +00:00
if ( ! empty ( $this -> belongsTo )) {
$keys = $this -> find ( 'first' , array ( 'fields' , $this -> __collectForeignKeys ()));
}
2006-12-19 19:26:02 +00:00
if ( $db -> delete ( $this )) {
2008-01-01 19:34:40 +00:00
if ( ! empty ( $this -> belongsTo )) {
$this -> updateCounterCache ( $keys [ $this -> alias ]);
}
2008-03-09 06:24:40 +00:00
$this -> Behaviors -> trigger ( $this , 'afterDelete' );
2006-07-30 12:24:03 +00:00
$this -> afterDelete ();
$this -> _clearCache ();
$this -> id = false ;
2007-10-01 16:49:37 +00:00
$this -> __exists = null ;
2006-07-30 12:24:03 +00:00
return true ;
}
}
return false ;
}
/**
2007-11-05 03:36:12 +00:00
* Synonym for del () .
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* @ param mixed $id ID of record to delete
* @ param boolean $cascade Set to true to delete records that depend on this record
2007-10-22 16:09:35 +00:00
* @ return boolean True on success
2007-11-05 03:36:12 +00:00
* @ access public
* @ see Model :: del ()
2006-07-30 12:24:03 +00:00
*/
function delete ( $id = null , $cascade = true ) {
return $this -> del ( $id , $cascade );
}
/**
2007-01-31 23:25:05 +00:00
* Cascades model deletes to hasMany and hasOne relationships .
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* @ param string $id ID of record that was deleted
* @ param boolean $cascade Set to true to delete records that depend on this record
2006-07-30 12:24:03 +00:00
* @ access protected
*/
2007-01-31 23:25:05 +00:00
function _deleteDependent ( $id , $cascade ) {
2007-03-26 17:16:43 +00:00
if ( ! empty ( $this -> __backAssociation )) {
2006-11-23 11:31:59 +00:00
$savedAssociatons = $this -> __backAssociation ;
2007-03-26 17:16:43 +00:00
$this -> __backAssociation = array ();
2006-11-23 11:31:59 +00:00
}
2007-12-08 06:08:03 +00:00
foreach ( array_merge ( $this -> hasMany , $this -> hasOne ) as $assoc => $data ) {
2006-07-30 12:24:03 +00:00
if ( $data [ 'dependent' ] === true && $cascade === true ) {
2007-01-31 23:25:05 +00:00
$model =& $this -> { $assoc };
2006-07-30 12:24:03 +00:00
$field = $model -> escapeField ( $data [ 'foreignKey' ]);
2007-01-31 23:25:05 +00:00
$model -> recursive = - 1 ;
2006-07-30 12:24:03 +00:00
2008-01-01 19:34:40 +00:00
if ( isset ( $data [ 'exclusive' ]) && $data [ 'exclusive' ]) {
$model -> deleteAll ( array ( $field => $id ));
} else {
2008-02-28 01:50:38 +00:00
$records = $model -> find ( 'all' , array ( 'conditions' => array ( $field => $id ), 'fields' => $model -> primaryKey ));
2008-01-01 19:34:40 +00:00
if ( ! empty ( $records )) {
foreach ( $records as $record ) {
$model -> delete ( $record [ $model -> alias ][ $model -> primaryKey ]);
}
2006-11-23 11:05:47 +00:00
}
2006-07-30 12:24:03 +00:00
}
}
}
2006-11-23 11:31:59 +00:00
if ( isset ( $savedAssociatons )) {
$this -> __backAssociation = $savedAssociatons ;
}
2006-07-30 12:24:03 +00:00
}
/**
2007-01-31 23:25:05 +00:00
* Cascades model deletes to HABTM join keys .
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* @ param string $id ID of record that was deleted
2006-07-30 12:24:03 +00:00
* @ access protected
*/
2007-01-31 23:25:05 +00:00
function _deleteLinks ( $id ) {
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-02-21 15:36:13 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $this -> hasAndBelongsToMany as $assoc => $data ) {
2008-03-11 02:44:33 +00:00
$records = $this -> { $data [ 'with' ]} -> find ( 'all' , array (
'conditions' => array ( $data [ 'foreignKey' ] => $id ),
'fields' => $this -> { $data [ 'with' ]} -> primaryKey ,
'recursive' => - 1
));
if ( ! empty ( $records )) {
foreach ( $records as $record ) {
$this -> { $data [ 'with' ]} -> delete ( $record [ $this -> { $data [ 'with' ]} -> alias ][ $this -> { $data [ 'with' ]} -> primaryKey ]);
2006-07-30 12:24:03 +00:00
}
}
}
}
2006-11-21 22:40:30 +00:00
/**
* Allows model records to be deleted based on a set of conditions
*
2007-11-05 03:36:12 +00:00
* @ param mixed $conditions Conditions to match
* @ param boolean $cascade Set to true to delete records that depend on this record
* @ param boolean $callbacks Run callbacks ( not being used )
2007-10-22 16:09:35 +00:00
* @ return boolean True on success , false on failure
2007-11-05 03:36:12 +00:00
* @ access public
2006-11-21 22:40:30 +00:00
*/
2007-12-25 10:37:08 +00:00
function deleteAll ( $conditions , $cascade = true , $callbacks = false ) {
2007-02-08 17:50:47 +00:00
if ( empty ( $conditions )) {
return false ;
}
2007-12-25 10:37:08 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2007-12-23 07:43:31 +00:00
2007-12-25 10:37:08 +00:00
if ( ! $cascade && ! $callbacks ) {
return $db -> delete ( $this , $conditions );
} else {
$ids = Set :: extract (
$this -> find ( 'all' , array_merge ( array ( 'fields' => " { $this -> alias } . { $this -> primaryKey } " , 'recursive' => 0 ), compact ( 'conditions' ))),
" { n}. { $this -> alias } . { $this -> primaryKey } "
);
2007-02-08 17:50:47 +00:00
2007-12-25 10:37:08 +00:00
if ( empty ( $ids )) {
return false ;
}
if ( $callbacks ) {
$_id = $this -> id ;
2007-12-03 00:43:11 +00:00
2007-12-25 10:37:08 +00:00
foreach ( $ids as $id ) {
$this -> delete ( $id , $cascade );
}
$this -> id = $_id ;
} else {
foreach ( $ids as $id ) {
$this -> _deleteLinks ( $id );
if ( $cascade ) {
$this -> _deleteDependent ( $id , $cascade );
}
}
return $db -> delete ( $this , array ( $this -> alias . '.' . $this -> primaryKey => $ids ));
2007-12-03 00:43:11 +00:00
}
2007-02-08 17:50:47 +00:00
}
2006-11-21 22:40:30 +00:00
}
2008-01-01 19:34:40 +00:00
/**
* Collects foreign keys from associations
*
* @ access private
*/
function __collectForeignKeys ( $type = 'belongsTo' ) {
$result = array ();
foreach ( $this -> { $type } as $assoc => $data ) {
if ( isset ( $data [ 'foreignKey' ]) && is_string ( $data [ 'foreignKey' ])) {
$result [ $assoc ] = $data [ 'foreignKey' ];
}
}
return $result ;
}
2006-07-30 12:24:03 +00:00
/**
* Returns true if a record with set id exists .
*
2007-10-22 16:54:36 +00:00
* @ param boolean $reset if true will force database query
2007-10-22 16:09:35 +00:00
* @ return boolean True if such a record exists
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-10-01 16:49:37 +00:00
function exists ( $reset = false ) {
2008-02-28 04:35:20 +00:00
if ( $this -> getID () === false || $this -> useTable === false ) {
2006-10-27 18:14:35 +00:00
return false ;
2006-07-30 12:24:03 +00:00
}
2007-10-01 16:49:37 +00:00
if ( $this -> __exists !== null && $reset !== true ) {
return $this -> __exists ;
}
2008-02-28 01:50:38 +00:00
return $this -> __exists = ( $this -> find ( 'count' , array (
'conditions' => array ( $this -> alias . '.' . $this -> primaryKey => $this -> getID ()), 'recursive' => - 1
)) > 0 );
2006-07-30 12:24:03 +00:00
}
/**
* Returns true if a record that meets given conditions exists
*
* @ param array $conditions SQL conditions array
2007-10-22 16:09:35 +00:00
* @ return boolean True if such a record exists
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function hasAny ( $conditions = null ) {
2007-10-21 18:34:23 +00:00
return ( $this -> find ( 'count' , array ( 'conditions' => $conditions , 'recursive' => - 1 )) != false );
2006-07-30 12:24:03 +00:00
}
/**
* Return a single row as a resultset array .
* By using the $recursive parameter , the call can access further " levels of association " than
* the ones this model is directly associated to .
*
2008-01-11 03:16:19 +00:00
* Eg : find ( array ( 'name' => 'Thomas Anderson' ), array ( 'name' , 'email' ), 'field3 DESC' , 2 );
2007-11-05 03:36:12 +00:00
*
* Also used to perform new - notation finds , where the first argument is type of find operation to perform
* ( all / first / count ), second parameter options for finding ( indexed array , including : 'conditions' , 'limit' ,
* 'recursive' , 'page' , 'fields' , 'offset' , 'order' )
*
* Eg : find ( 'all' , array (
2008-01-11 03:16:19 +00:00
* 'conditions' => array ( 'name' => 'Thomas Anderson' ),
2007-11-05 03:36:12 +00:00
* 'fields' => array ( 'name' , 'email' ),
* 'order' => 'field3 DESC' ,
* 'recursive' => 2 ));
*
2008-01-11 03:16:19 +00:00
* Specifying 'fields' for new - notation 'list' :
* - If no fields are specified , then 'id' is used for key and 'model->displayField' is used for value .
* - If a single field is specified , 'id' is used for key and specified field is used for value .
* - If three fields are specified , they are used ( in order ) for key , value and group .
* - Otherwise , first and second fields are used for key and value .
*
2007-11-05 03:36:12 +00:00
* @ param array $conditions SQL conditions array , or type of find operation ( all / first / count )
* @ param mixed $fields Either a single string of a field name , or an array of field names , or options for matching
2006-07-30 12:24:03 +00:00
* @ param string $order SQL ORDER BY conditions ( e . g . " price DESC " or " name ASC " )
2007-10-22 16:11:12 +00:00
* @ param integer $recursive The number of levels deep to fetch associated records
2006-07-30 12:24:03 +00:00
* @ return array Array of records
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-12-10 03:28:21 +00:00
function find ( $conditions = null , $fields = array (), $order = null , $recursive = null ) {
2007-10-21 18:34:23 +00:00
if ( ! is_string ( $conditions ) || ( is_string ( $conditions ) && ! array_key_exists ( $conditions , $this -> __findMethods ))) {
$type = 'first' ;
2007-12-08 06:08:03 +00:00
$query = array_merge ( compact ( 'conditions' , 'fields' , 'order' , 'recursive' ), array ( 'limit' => 1 ));
2007-10-21 18:34:23 +00:00
} else {
2008-02-28 01:50:38 +00:00
list ( $type , $query ) = array ( $conditions , $fields );
2006-07-30 12:24:03 +00:00
}
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-02-28 01:50:38 +00:00
$this -> findQueryType = $type ;
2006-07-30 12:24:03 +00:00
$this -> id = $this -> getID ();
2007-12-08 06:08:03 +00:00
$query = array_merge (
2007-10-21 18:34:23 +00:00
array (
'conditions' => null , 'fields' => null , 'joins' => array (),
'limit' => null , 'offset' => null , 'order' => null , 'page' => null
),
$query
);
2008-02-28 01:50:38 +00:00
if ( $type != 'all' ) {
if ( $this -> __findMethods [ $type ] === true ) {
$query = $this -> { '_find' . ucfirst ( $type )}( 'before' , $query );
}
2007-10-21 18:34:23 +00:00
}
if ( ! is_numeric ( $query [ 'page' ]) || intval ( $query [ 'page' ]) < 1 ) {
$query [ 'page' ] = 1 ;
2006-11-08 03:28:24 +00:00
}
2008-02-28 01:50:38 +00:00
if ( $query [ 'page' ] > 1 && ! empty ( $query [ 'limit' ])) {
2007-10-21 18:34:23 +00:00
$query [ 'offset' ] = ( $query [ 'page' ] - 1 ) * $query [ 'limit' ];
2006-07-30 12:24:03 +00:00
}
2008-02-28 01:50:38 +00:00
if ( $query [ 'order' ] === null && $this -> order !== null ) {
$query [ 'order' ] = $this -> order ;
2006-07-30 12:24:03 +00:00
}
2008-02-28 01:50:38 +00:00
$query [ 'order' ] = array ( $query [ 'order' ]);
2006-07-30 12:24:03 +00:00
2008-03-09 06:24:40 +00:00
$return = $this -> Behaviors -> trigger ( $this , 'beforeFind' , array ( $query ), array ( 'break' => true , 'breakOn' => false , 'modParams' => true ));
2008-02-21 15:36:13 +00:00
$query = ife ( is_array ( $return ), $return , $query );
2008-02-28 01:50:38 +00:00
2008-02-21 15:36:13 +00:00
if ( $return === false ) {
return null ;
2006-07-30 12:24:03 +00:00
}
2007-10-21 18:34:23 +00:00
$return = $this -> beforeFind ( $query );
2008-02-21 15:36:13 +00:00
$query = ife ( is_array ( $return ), $return , $query );
2008-02-28 01:50:38 +00:00
2008-02-21 15:36:13 +00:00
if ( $return === false ) {
2006-07-30 12:24:03 +00:00
return null ;
}
2008-02-21 15:36:13 +00:00
2007-10-21 18:34:23 +00:00
$results = $db -> read ( $this , $query );
2008-05-17 16:23:46 +00:00
$this -> resetAssociations ();
2007-12-25 10:37:08 +00:00
$this -> findQueryType = null ;
2007-10-21 18:34:23 +00:00
2008-02-28 01:50:38 +00:00
if ( $type === 'all' ) {
return $this -> __filterResults ( $results );
} else {
if ( $this -> __findMethods [ $type ] === true ) {
return $this -> { '_find' . ucfirst ( $type )}( 'after' , $query , $results );
}
}
}
/**
* Handles the before / after filter logic for find ( 'first' ) operations . Only called by Model :: find () .
*
* @ param string $state Either " before " or " after "
* @ param array $query
* @ param array $data
* @ return array
* @ access protected
*/
function _findFirst ( $state , $query , $results = array ()) {
if ( $state == 'before' ) {
$query [ 'limit' ] = 1 ;
if ( empty ( $query [ 'conditions' ]) && ! empty ( $this -> id )) {
$query [ 'conditions' ] = array ( $this -> escapeField () => $this -> id );
}
return $query ;
} elseif ( $state == 'after' ) {
$results = $this -> __filterResults ( $results );
if ( empty ( $results [ 0 ])) {
2007-10-21 18:34:23 +00:00
return false ;
2008-02-28 01:50:38 +00:00
}
return $results [ 0 ];
2007-10-21 18:34:23 +00:00
}
}
/**
2008-02-28 01:50:38 +00:00
* Handles the before / after filter logic for find ( 'count' ) operations . Only called by Model :: find () .
2007-10-21 18:34:23 +00:00
*
2008-02-28 01:50:38 +00:00
* @ param string $state Either " before " or " after "
* @ param array $query
* @ param array $data
* @ return int The number of records found , or false
* @ access protected
*/
function _findCount ( $state , $query , $results = array ()) {
if ( $state == 'before' ) {
if ( empty ( $query [ 'fields' ])) {
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-03-13 03:11:29 +00:00
$query [ 'fields' ] = $db -> calculate ( $this , 'count' );
2008-02-28 01:50:38 +00:00
}
$query [ 'order' ] = false ;
return $query ;
} elseif ( $state == 'after' ) {
if ( isset ( $results [ 0 ][ 0 ][ 'count' ])) {
return intval ( $results [ 0 ][ 0 ][ 'count' ]);
} elseif ( isset ( $results [ 0 ][ $this -> alias ][ 'count' ])) {
return intval ( $results [ 0 ][ $this -> alias ][ 'count' ]);
}
return false ;
}
}
/**
* Handles the before / after filter logic for find ( 'list' ) operations . Only called by Model :: find () .
*
* @ param string $state Either " before " or " after "
* @ param array $query
* @ param array $data
* @ return array Key / value pairs of primary keys / display field values of all records found
* @ access protected
*/
function _findList ( $state , $query , $results = array ()) {
if ( $state == 'before' ) {
if ( empty ( $query [ 'fields' ])) {
$query [ 'fields' ] = array ( " { $this -> alias } . { $this -> primaryKey } " , " { $this -> alias } . { $this -> displayField } " );
$list = array ( " { n}. { $this -> alias } . { $this -> primaryKey } " , " { n}. { $this -> alias } . { $this -> displayField } " , null );
} else {
if ( ! is_array ( $query [ 'fields' ])) {
$query [ 'fields' ] = String :: tokenize ( $query [ 'fields' ]);
}
if ( count ( $query [ 'fields' ]) == 1 ) {
$list = array ( " { n}. { $this -> alias } . { $this -> primaryKey } " , '{n}.' . $query [ 'fields' ][ 0 ], null );
$query [ 'fields' ] = array ( " { $this -> alias } . { $this -> primaryKey } " , $query [ 'fields' ][ 0 ]);
} elseif ( count ( $query [ 'fields' ]) == 3 ) {
$list = array ( '{n}.' . $query [ 'fields' ][ 0 ], '{n}.' . $query [ 'fields' ][ 1 ], '{n}.' . $query [ 'fields' ][ 2 ]);
} else {
$list = array ( '{n}.' . $query [ 'fields' ][ 0 ], '{n}.' . $query [ 'fields' ][ 1 ], null );
}
}
if ( ! isset ( $query [ 'recursive' ]) || $query [ 'recursive' ] === null ) {
$query [ 'recursive' ] = - 1 ;
}
list ( $query [ 'list' ][ 'keyPath' ], $query [ 'list' ][ 'valuePath' ], $query [ 'list' ][ 'groupPath' ]) = $list ;
return $query ;
} elseif ( $state == 'after' ) {
if ( empty ( $results )) {
return array ();
}
return Set :: combine (
$this -> __filterResults ( $results ),
$query [ 'list' ][ 'keyPath' ],
$query [ 'list' ][ 'valuePath' ],
$query [ 'list' ][ 'groupPath' ]
);
}
}
/**
* @ deprecated
* @ see Model :: find ( 'all' )
2007-10-21 18:34:23 +00:00
*/
function findAll ( $conditions = null , $fields = null , $order = null , $limit = null , $page = 1 , $recursive = null ) {
2008-02-28 01:50:38 +00:00
//trigger_error(__('(Model::findAll) Deprecated, use Model::find("all")', true), E_USER_WARNING);
2007-10-21 18:34:23 +00:00
return $this -> find ( 'all' , compact ( 'conditions' , 'fields' , 'order' , 'limit' , 'page' , 'recursive' ));
}
/**
* Passes query results through model and behavior afterFilter () methods
*
2007-11-05 03:36:12 +00:00
* @ param array Results to filter
* @ param boolean $primary If this is the primary model results ( results from model where the find operation was performed )
* @ return array Set of filtered results
2007-10-21 18:34:23 +00:00
* @ access private
*/
function __filterResults ( $results , $primary = true ) {
2008-03-09 06:24:40 +00:00
$return = $this -> Behaviors -> trigger ( $this , 'afterFind' , array ( $results , $primary ), array ( 'modParams' => true ));
2008-02-21 15:36:13 +00:00
if ( $return !== true ) {
$results = $return ;
2006-07-30 12:24:03 +00:00
}
2007-10-21 18:34:23 +00:00
return $this -> afterFind ( $results , $primary );
2006-07-30 12:24:03 +00:00
}
/**
* Method is called only when bindTo < ModelName > () is used .
* This resets the association arrays for the model back
* to the original as set in the model .
*
2007-11-05 03:36:12 +00:00
* @ return boolean Success
2008-05-17 16:23:46 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-05-17 16:23:46 +00:00
function resetAssociations () {
2007-10-22 02:11:58 +00:00
if ( ! empty ( $this -> __backAssociation )) {
foreach ( $this -> __associations as $type ) {
if ( isset ( $this -> __backAssociation [ $type ])) {
$this -> { $type } = $this -> __backAssociation [ $type ];
}
2006-07-30 12:24:03 +00:00
}
2007-10-22 02:11:58 +00:00
$this -> __backAssociation = array ();
2006-07-30 12:24:03 +00:00
}
2007-10-22 02:11:58 +00:00
foreach ( $this -> __associations as $type ) {
foreach ( $this -> { $type } as $key => $name ) {
if ( ! empty ( $this -> { $key } -> __backAssociation )) {
2008-05-17 16:23:46 +00:00
$this -> { $key } -> resetAssociations ();
2007-10-22 02:11:58 +00:00
}
}
}
2007-03-26 17:16:43 +00:00
$this -> __backAssociation = array ();
2006-07-30 12:24:03 +00:00
return true ;
}
/**
2008-03-01 03:12:12 +00:00
* @ deprecated
* @ see Model :: query
2006-07-30 12:24:03 +00:00
*/
function execute ( $data ) {
2008-03-01 03:12:12 +00:00
trigger_error ( __ ( '(Model::execute) Deprecated, use Model::query' , true ), E_USER_WARNING );
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-07-30 12:24:03 +00:00
$data = $db -> fetchAll ( $data , $this -> cacheQueries );
2007-06-20 06:15:35 +00:00
foreach ( $data as $key => $value ) {
foreach ( $this -> tableToModel as $key1 => $value1 ) {
2006-07-30 12:24:03 +00:00
if ( isset ( $data [ $key ][ $key1 ])) {
$newData [ $key ][ $value1 ] = $data [ $key ][ $key1 ];
}
}
}
if ( ! empty ( $newData )) {
return $newData ;
}
return $data ;
}
/**
2008-02-28 01:50:38 +00:00
* @ deprecated
* @ see Model :: find ( 'count' )
2006-07-30 12:24:03 +00:00
*/
function findCount ( $conditions = null , $recursive = 0 ) {
2008-02-28 01:50:38 +00:00
//trigger_error(__('(Model::findCount) Deprecated, use Model::find("count")', true), E_USER_WARNING);
2007-10-21 18:34:23 +00:00
return $this -> find ( 'count' , compact ( 'conditions' , 'recursive' ));
2006-07-30 12:24:03 +00:00
}
2007-01-22 08:09:17 +00:00
/**
* False if any fields passed match any ( by default , all if $or = false ) of their matching values .
*
* @ param array $fields Field / value pairs to search ( if no values specified , they are pulled from $this -> data )
2007-10-22 16:54:36 +00:00
* @ param boolean $or If false , all fields specified must match in order for a false return value
2007-10-22 16:09:35 +00:00
* @ return boolean False if any records matching any fields are found
2007-11-05 03:36:12 +00:00
* @ access public
2007-01-22 08:09:17 +00:00
*/
function isUnique ( $fields , $or = true ) {
if ( ! is_array ( $fields )) {
$fields = func_get_args ();
if ( is_bool ( $fields [ count ( $fields ) - 1 ])) {
$or = $fields [ count ( $fields ) - 1 ];
unset ( $fields [ count ( $fields ) - 1 ]);
}
}
foreach ( $fields as $field => $value ) {
if ( is_numeric ( $field )) {
unset ( $fields [ $field ]);
$field = $value ;
2007-10-27 01:32:17 +00:00
if ( isset ( $this -> data [ $this -> alias ][ $field ])) {
$value = $this -> data [ $this -> alias ][ $field ];
2007-01-22 08:09:17 +00:00
} else {
$value = null ;
}
}
2007-01-24 10:47:12 +00:00
2007-01-22 08:09:17 +00:00
if ( strpos ( $field , '.' ) === false ) {
unset ( $fields [ $field ]);
2007-10-27 01:32:17 +00:00
$fields [ $this -> alias . '.' . $field ] = $value ;
2007-01-22 08:09:17 +00:00
}
}
if ( $or ) {
$fields = array ( 'or' => $fields );
}
2008-01-08 06:29:51 +00:00
if ( ! empty ( $this -> id )) {
$fields [ $this -> alias . '.' . $this -> primaryKey ] = '!= ' . $this -> id ;
}
2008-01-01 19:34:40 +00:00
return ( $this -> find ( 'count' , array ( 'conditions' => $fields )) == 0 );
2007-01-22 08:09:17 +00:00
}
2006-07-30 12:24:03 +00:00
/**
* Special findAll variation for tables joined to themselves .
* The table needs the fields id and parent_id to work .
*
* @ param array $conditions Conditions for the findAll () call
* @ param array $fields Fields for the findAll () call
* @ param string $sort SQL ORDER BY statement
2007-11-05 03:36:12 +00:00
* @ return array Threaded results
* @ access public
2006-07-30 12:24:03 +00:00
* @ todo Perhaps create a Component with this logic
*/
function findAllThreaded ( $conditions = null , $fields = null , $sort = null ) {
return $this -> __doThread ( Model :: findAll ( $conditions , $fields , $sort ), null );
}
/**
* Private , recursive helper method for findAllThreaded .
*
2007-11-05 03:36:12 +00:00
* @ param array $data Results of find operation
2006-07-30 12:24:03 +00:00
* @ param string $root NULL or id for root node of operation
2007-11-05 03:36:12 +00:00
* @ return array Threaded results
2006-07-30 12:24:03 +00:00
* @ access private
2007-11-05 03:36:12 +00:00
* @ see Model :: findAllThreaded ()
2006-07-30 12:24:03 +00:00
*/
2008-01-09 01:06:45 +00:00
function __doThread ( $data , $root ) {
2006-07-30 12:24:03 +00:00
$out = array ();
$sizeOf = sizeof ( $data );
2008-01-09 01:06:45 +00:00
for ( $ii = 0 ; $ii < $sizeOf ; $ii ++ ) {
2007-10-27 01:32:17 +00:00
if (( $data [ $ii ][ $this -> alias ][ 'parent_id' ] == $root ) || (( $root === null ) && ( $data [ $ii ][ $this -> alias ][ 'parent_id' ] == '0' ))) {
2006-07-30 12:24:03 +00:00
$tmp = $data [ $ii ];
2007-10-27 01:32:17 +00:00
if ( isset ( $data [ $ii ][ $this -> alias ][ $this -> primaryKey ])) {
2008-01-09 01:06:45 +00:00
$tmp [ 'children' ] = $this -> __doThread ( $data , $data [ $ii ][ $this -> alias ][ $this -> primaryKey ]);
2006-07-30 12:24:03 +00:00
} else {
$tmp [ 'children' ] = null ;
}
$out [] = $tmp ;
}
}
return $out ;
}
/**
* Returns an array with keys " prev " and " next " that holds the id ' s of neighbouring data ,
* which is useful when creating paged lists .
*
* @ param string $conditions SQL conditions for matching rows
2008-02-28 01:50:38 +00:00
* @ param string $field Field name ( parameter for find ())
2007-11-05 03:36:12 +00:00
* @ param integer $value Value from where to find neighbours
2006-07-30 12:24:03 +00:00
* @ return array Array with keys " prev " and " next " that holds the id ' s
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function findNeighbours ( $conditions = null , $field , $value ) {
2007-12-08 06:08:03 +00:00
$conditions = ( array ) $conditions ;
2007-10-23 13:41:25 +00:00
if ( is_array ( $field )) {
$fields = $field ;
$field = $fields [ 0 ];
} else {
$fields = $field ;
}
2007-10-21 18:34:23 +00:00
$prev = $next = null ;
2006-07-30 12:24:03 +00:00
2008-05-19 20:25:42 +00:00
$result = $this -> findAll ( array_filter ( array_merge ( $conditions , array ( $field => '< ' . $value ))), $fields , $field . ' DESC' , 1 , null , 0 );
if ( isset ( $result [ 0 ])) {
$prev = $result [ 0 ];
}
$result = $this -> findAll ( array_filter ( array_merge ( $conditions , array ( $field => '> ' . $value ))), $fields , $field . ' ASC' , 1 , null , 0 );
if ( isset ( $result [ 0 ])) {
$next = $result [ 0 ];
}
2007-10-21 18:34:23 +00:00
return compact ( 'prev' , 'next' );
2006-07-30 12:24:03 +00:00
}
/**
* Returns a resultset for given SQL statement . Generic SQL queries should be made with this method .
*
* @ param string $sql SQL statement
* @ return array Resultset
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function query () {
$params = func_get_args ();
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-07-30 12:24:03 +00:00
return call_user_func_array ( array ( & $db , 'query' ), $params );
}
/**
* Returns true if all fields pass validation , otherwise false .
*
2008-05-13 23:34:59 +00:00
* @ param string $options An optional array of custom options to be made available in the beforeValidate callback
2007-10-22 16:09:35 +00:00
* @ return boolean True if there are no errors
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-05-13 23:34:59 +00:00
function validates ( $options = array ()) {
$errors = $this -> invalidFields ( $options );
2006-10-10 22:29:13 +00:00
if ( is_array ( $errors )) {
return count ( $errors ) === 0 ;
}
return $errors ;
2006-07-30 12:24:03 +00:00
}
/**
2007-11-05 03:36:12 +00:00
* Returns an array of fields that do not meet validation .
2006-07-30 12:24:03 +00:00
*
2008-05-13 23:34:59 +00:00
* @ param string $options An optional array of custom options to be made available in the beforeValidate callback
2007-09-20 15:16:25 +00:00
* @ return array Array of invalid fields
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-05-13 23:34:59 +00:00
function invalidFields ( $options = array ()) {
if ( ! $this -> Behaviors -> trigger ( $this , 'beforeValidate' , array ( $options ), array ( 'break' => true , 'breakOn' => false )) || $this -> beforeValidate ( $options ) === false ) {
2007-03-17 02:59:48 +00:00
return $this -> validationErrors ;
}
2006-12-25 06:53:19 +00:00
if ( ! isset ( $this -> validate ) || empty ( $this -> validate )) {
2007-02-02 18:05:48 +00:00
return $this -> validationErrors ;
2006-07-30 12:24:03 +00:00
}
2008-02-21 15:36:13 +00:00
$data = $this -> data ;
$methods = array_map ( 'strtolower' , get_class_methods ( $this ));
2008-02-24 02:47:10 +00:00
$behaviorMethods = array_keys ( $this -> Behaviors -> methods ());
2006-07-30 12:24:03 +00:00
2007-10-27 01:32:17 +00:00
if ( isset ( $data [ $this -> alias ])) {
$data = $data [ $this -> alias ];
2008-03-19 01:34:23 +00:00
} elseif ( ! is_array ( $data )) {
$data = array ();
2006-07-30 12:24:03 +00:00
}
2007-12-22 21:03:30 +00:00
$Validation =& Validation :: getInstance ();
2007-10-01 16:49:37 +00:00
$exists = $this -> exists ();
2006-12-25 06:53:19 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $this -> validate as $fieldName => $ruleSet ) {
2007-04-30 06:24:33 +00:00
if ( ! is_array ( $ruleSet ) || ( is_array ( $ruleSet ) && isset ( $ruleSet [ 'rule' ]))) {
$ruleSet = array ( $ruleSet );
2007-01-06 04:58:24 +00:00
}
2008-02-21 15:36:13 +00:00
$default = array ( 'allowEmpty' => null , 'required' => null , 'rule' => 'blank' , 'last' => false , 'on' => null );
2006-12-25 06:53:19 +00:00
2007-04-30 12:10:57 +00:00
foreach ( $ruleSet as $index => $validator ) {
2007-04-30 06:24:33 +00:00
if ( ! is_array ( $validator )) {
$validator = array ( 'rule' => $validator );
}
2007-12-08 06:08:03 +00:00
$validator = array_merge ( $default , $validator );
2007-04-30 06:24:33 +00:00
2007-04-30 12:10:57 +00:00
if ( isset ( $validator [ 'message' ])) {
$message = $validator [ 'message' ];
} else {
2007-10-01 16:49:37 +00:00
$message = __ ( 'This field cannot be left blank' , true );
2007-04-30 12:10:57 +00:00
}
2007-08-29 20:47:03 +00:00
if ( empty ( $validator [ 'on' ]) || ( $validator [ 'on' ] == 'create' && ! $exists ) || ( $validator [ 'on' ] == 'update' && $exists )) {
2007-05-04 07:13:15 +00:00
if (( ! isset ( $data [ $fieldName ]) && $validator [ 'required' ] === true ) || ( isset ( $data [ $fieldName ]) && ( empty ( $data [ $fieldName ]) && ! is_numeric ( $data [ $fieldName ])) && $validator [ 'allowEmpty' ] === false )) {
2007-04-30 12:10:57 +00:00
$this -> invalidate ( $fieldName , $message );
2007-12-24 22:14:31 +00:00
if ( $validator [ 'last' ]) {
break ;
}
2007-09-20 22:45:00 +00:00
} elseif ( array_key_exists ( $fieldName , $data )) {
2007-06-20 06:15:35 +00:00
if ( empty ( $data [ $fieldName ]) && $data [ $fieldName ] != '0' && $validator [ 'allowEmpty' ] === true ) {
2007-05-01 03:52:28 +00:00
break ;
}
2007-04-30 06:24:33 +00:00
if ( is_array ( $validator [ 'rule' ])) {
$rule = $validator [ 'rule' ][ 0 ];
unset ( $validator [ 'rule' ][ 0 ]);
2007-12-08 06:08:03 +00:00
$ruleParams = array_merge ( array ( $data [ $fieldName ]), array_values ( $validator [ 'rule' ]));
2007-04-30 06:24:33 +00:00
} else {
$rule = $validator [ 'rule' ];
$ruleParams = array ( $data [ $fieldName ]);
}
$valid = true ;
2007-05-05 17:36:30 +00:00
2008-02-21 15:36:13 +00:00
if ( in_array ( strtolower ( $rule ), $methods )) {
$ruleParams [] = array_diff_key ( $validator , $default );
$ruleParams [ 0 ] = array ( $fieldName => $ruleParams [ 0 ]);
$valid = $this -> dispatchMethod ( $rule , $ruleParams );
} elseif ( in_array ( $rule , $behaviorMethods ) || in_array ( strtolower ( $rule ), $behaviorMethods )) {
2007-05-02 07:48:22 +00:00
$ruleParams [] = array_diff_key ( $validator , $default );
2007-12-25 04:53:15 +00:00
$ruleParams [ 0 ] = array ( $fieldName => $ruleParams [ 0 ]);
2008-03-09 06:24:40 +00:00
$valid = $this -> Behaviors -> dispatchMethod ( $this , $rule , $ruleParams );
2007-04-30 06:24:33 +00:00
} elseif ( method_exists ( $Validation , $rule )) {
2008-02-21 15:36:13 +00:00
$valid = $Validation -> dispatchMethod ( $rule , $ruleParams );
2007-04-30 06:24:33 +00:00
} elseif ( ! is_array ( $validator [ 'rule' ])) {
$valid = preg_match ( $rule , $data [ $fieldName ]);
}
if ( ! $valid ) {
2007-04-30 12:10:57 +00:00
if ( ! isset ( $validator [ 'message' ])) {
2007-05-14 10:37:23 +00:00
if ( is_string ( $index )) {
$validator [ 'message' ] = $index ;
} else {
$validator [ 'message' ] = ife ( is_numeric ( $index ) && count ( $ruleSet ) > 1 , ( $index + 1 ), $message );
}
2007-04-30 12:10:57 +00:00
}
2007-04-30 06:24:33 +00:00
$this -> invalidate ( $fieldName , $validator [ 'message' ]);
2008-02-21 15:36:13 +00:00
2007-12-19 02:05:15 +00:00
if ( $validator [ 'last' ]) {
break ;
}
2007-04-30 06:24:33 +00:00
}
2006-12-25 06:53:19 +00:00
}
}
2006-07-30 12:24:03 +00:00
}
}
return $this -> validationErrors ;
}
/**
2007-11-05 03:36:12 +00:00
* Sets a field as invalid , optionally setting the name of validation
* rule ( in case of multiple validation for field ) that was broken
2006-07-30 12:24:03 +00:00
*
* @ param string $field The name of the field to invalidate
2007-11-05 03:36:12 +00:00
* @ param string $value Name of validation rule that was not met
* @ access public
2006-07-30 12:24:03 +00:00
*/
2008-02-21 15:36:13 +00:00
function invalidate ( $field , $value = true ) {
2006-07-30 12:24:03 +00:00
if ( ! is_array ( $this -> validationErrors )) {
$this -> validationErrors = array ();
}
2007-10-21 03:44:26 +00:00
$this -> validationErrors [ $field ] = $value ;
2006-07-30 12:24:03 +00:00
}
/**
* Returns true if given field name is a foreign key in this Model .
*
* @ param string $field Returns true if the input string ends in " _id "
2007-11-05 03:36:12 +00:00
* @ return boolean True if the field is a foreign key listed in the belongsTo array .
* @ access public
2006-07-30 12:24:03 +00:00
*/
function isForeignKey ( $field ) {
$foreignKeys = array ();
2007-11-05 03:36:12 +00:00
if ( ! empty ( $this -> belongsTo )) {
2007-06-20 06:15:35 +00:00
foreach ( $this -> belongsTo as $assoc => $data ) {
2006-07-30 12:24:03 +00:00
$foreignKeys [] = $data [ 'foreignKey' ];
}
}
2007-11-05 03:36:12 +00:00
return in_array ( $field , $foreignKeys );
2006-07-30 12:24:03 +00:00
}
/**
* Gets the display field for this model
*
* @ return string The name of the display field for this Model ( i . e . 'name' , 'title' ) .
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getDisplayField () {
return $this -> displayField ;
}
/**
2008-01-10 19:50:33 +00:00
* @ deprecated
2006-07-30 12:24:03 +00:00
*/
2006-07-30 23:31:04 +00:00
function generateList ( $conditions = null , $order = null , $limit = null , $keyPath = null , $valuePath = null , $groupPath = null ) {
2008-01-01 19:34:40 +00:00
trigger_error ( __ ( '(Model::generateList) Deprecated, use Model::find("list") or Model::find("all") and Set::combine()' , true ), E_USER_WARNING );
2006-07-30 23:31:04 +00:00
if ( $keyPath == null && $valuePath == null && $groupPath == null && $this -> hasField ( $this -> displayField )) {
2006-07-30 12:24:03 +00:00
$fields = array ( $this -> primaryKey , $this -> displayField );
} else {
$fields = null ;
}
2006-11-22 19:36:59 +00:00
$recursive = $this -> recursive ;
2007-06-20 06:15:35 +00:00
if ( $groupPath == null && $recursive >= 1 ) {
2006-10-18 01:07:09 +00:00
$this -> recursive = - 1 ;
2007-06-20 07:51:52 +00:00
} elseif ( $groupPath && $recursive >= 1 ) {
2006-11-03 19:49:32 +00:00
$this -> recursive = 0 ;
2006-10-18 01:07:09 +00:00
}
$result = $this -> findAll ( $conditions , $fields , $order , $limit );
2006-11-22 19:36:59 +00:00
$this -> recursive = $recursive ;
2006-12-26 16:17:37 +00:00
2007-06-20 06:15:35 +00:00
if ( ! $result ) {
2006-12-04 06:59:56 +00:00
return false ;
}
2006-07-30 12:24:03 +00:00
if ( $keyPath == null ) {
2008-01-01 19:34:40 +00:00
$keyPath = " { n}. { $this -> alias } . { $this -> primaryKey } " ;
2006-07-30 12:24:03 +00:00
}
if ( $valuePath == null ) {
2008-01-01 19:34:40 +00:00
$valuePath = " { n}. { $this -> alias } . { $this -> displayField } " ;
2006-07-30 12:24:03 +00:00
}
2007-07-01 03:43:05 +00:00
return Set :: combine ( $result , $keyPath , $valuePath , $groupPath );
2006-07-30 12:24:03 +00:00
}
/**
* Escapes the field name and prepends the model name . Escaping will be done according to the current database driver ' s rules .
*
2007-11-05 03:36:12 +00:00
* @ param string $field Field to escape ( e . g : id )
* @ param string $alias Alias for the model ( e . g : Post )
2006-07-30 12:24:03 +00:00
* @ return string The name of the escaped field for this Model ( i . e . id becomes `Post` . `id` ) .
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-02-08 17:50:47 +00:00
function escapeField ( $field = null , $alias = null ) {
if ( empty ( $alias )) {
2007-10-27 01:32:17 +00:00
$alias = $this -> alias ;
2006-07-30 12:24:03 +00:00
}
2007-02-08 17:50:47 +00:00
if ( empty ( $field )) {
$field = $this -> primaryKey ;
}
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2007-12-25 10:37:08 +00:00
if ( strpos ( $field , $db -> name ( $alias )) === 0 ) {
return $field ;
}
2008-02-21 15:36:13 +00:00
return $db -> name ( $alias . '.' . $field );
2006-07-30 12:24:03 +00:00
}
/**
* Returns the current record ' s ID
*
2007-11-05 03:36:12 +00:00
* @ param integer $list Index on which the composed ID is located
* @ return mixed The ID of the current record , false if no ID
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getID ( $list = 0 ) {
2006-12-12 22:06:22 +00:00
if ( empty ( $this -> id ) || ( is_array ( $this -> id ) && isset ( $this -> id [ 0 ]) && empty ( $this -> id [ 0 ]))) {
2006-10-27 18:14:35 +00:00
return false ;
}
2006-07-30 12:24:03 +00:00
if ( ! is_array ( $this -> id )) {
return $this -> id ;
}
2007-11-05 03:36:12 +00:00
if ( empty ( $this -> id )) {
2006-07-30 12:24:03 +00:00
return false ;
}
2006-12-12 22:06:22 +00:00
if ( isset ( $this -> id [ $list ]) && ! empty ( $this -> id [ $list ])) {
2006-07-30 12:24:03 +00:00
return $this -> id [ $list ];
2006-12-12 22:06:22 +00:00
} elseif ( isset ( $this -> id [ $list ])) {
return false ;
2006-07-30 12:24:03 +00:00
}
2007-06-20 06:15:35 +00:00
foreach ( $this -> id as $id ) {
2006-07-30 12:24:03 +00:00
return $id ;
}
return false ;
}
2008-02-28 01:50:38 +00:00
/**
* Top secret
*
* @ access public
*/
2007-11-18 06:14:27 +00:00
function normalizeFindParams ( $type , $data , $altType = null , $r = array (), $_this = null ) {
2007-11-17 22:46:55 +00:00
if ( $_this == null ) {
$_this = $this ;
$root = true ;
}
foreach (( array ) $data as $name => $children ) {
if ( is_numeric ( $name )) {
$name = $children ;
$children = array ();
}
if ( strpos ( $name , '.' ) !== false ) {
$chain = explode ( '.' , $name );
$name = array_shift ( $chain );
$children = array ( join ( '.' , $chain ) => $children );
}
if ( ! empty ( $children )) {
if ( $_this -> name == $name ) {
2007-12-08 06:08:03 +00:00
$r = array_merge ( $r , $this -> normalizeFindParams ( $type , $children , $altType , $r , $_this ));
2007-11-17 22:46:55 +00:00
} else {
2007-11-18 06:14:27 +00:00
if ( ! $_this -> getAssociated ( $name )) {
$r [ $altType ][ $name ] = $children ;
} else {
$r [ $name ] = $this -> normalizeFindParams ( $type , $children , $altType , @ $r [ $name ], $_this -> { $name });;
}
2007-11-17 22:46:55 +00:00
}
} else {
if ( $_this -> getAssociated ( $name )) {
$r [ $name ] = array ( $type => null );
} else {
2007-11-18 06:14:27 +00:00
if ( $altType != null ) {
$r [ $type ][] = $name ;
} else {
$r [ $type ] = $name ;
}
2007-11-17 22:46:55 +00:00
}
}
}
if ( isset ( $root )) {
return array ( $this -> name => $r );
}
return $r ;
}
2006-07-30 12:24:03 +00:00
/**
* Returns the ID of the last record this Model inserted
*
2007-11-05 03:36:12 +00:00
* @ return mixed Last inserted ID
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getLastInsertID () {
return $this -> getInsertID ();
}
/**
* Returns the ID of the last record this Model inserted
*
2007-11-05 03:36:12 +00:00
* @ return mixed Last inserted ID
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getInsertID () {
return $this -> __insertID ;
}
2006-10-20 17:24:37 +00:00
/**
* Sets the ID of the last record this Model inserted
*
2007-11-05 03:36:12 +00:00
* @ param mixed Last inserted ID
* @ access public
2006-10-20 17:24:37 +00:00
*/
function setInsertID ( $id ) {
$this -> __insertID = $id ;
}
2006-07-30 12:24:03 +00:00
/**
* Returns the number of rows returned from the last query
*
2007-11-05 03:36:12 +00:00
* @ return int Number of rows
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getNumRows () {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-07-30 12:24:03 +00:00
return $db -> lastNumRows ();
}
/**
* Returns the number of rows affected by the last query
*
2007-11-05 03:36:12 +00:00
* @ return int Number of rows
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getAffectedRows () {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-07-30 12:24:03 +00:00
return $db -> lastAffected ();
}
/**
* Sets the DataSource to which this model is bound
*
* @ param string $dataSource The name of the DataSource , as defined in Connections . php
2007-10-22 16:09:35 +00:00
* @ return boolean True on success
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function setDataSource ( $dataSource = null ) {
2008-03-01 05:15:53 +00:00
$oldConfig = $this -> useDbConfig ;
2007-01-10 11:15:43 +00:00
if ( $dataSource != null ) {
$this -> useDbConfig = $dataSource ;
2006-07-30 12:24:03 +00:00
}
2007-01-10 11:15:43 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2008-03-01 20:30:01 +00:00
if ( ! empty ( $oldConfig ) && isset ( $db -> config [ 'prefix' ])) {
2008-03-01 05:15:53 +00:00
$oldDb =& ConnectionManager :: getDataSource ( $oldConfig );
2006-07-30 12:24:03 +00:00
2008-03-01 20:30:01 +00:00
if ( empty ( $this -> tablePrefix ) || ( ! isset ( $oldDb -> config [ 'prefix' ]) || $this -> tablePrefix == $oldDb -> config [ 'prefix' ])) {
2008-03-01 05:15:53 +00:00
$this -> tablePrefix = $db -> config [ 'prefix' ];
}
2008-03-01 20:26:42 +00:00
} elseif ( isset ( $db -> config [ 'prefix' ])) {
2006-07-30 12:24:03 +00:00
$this -> tablePrefix = $db -> config [ 'prefix' ];
}
if ( empty ( $db ) || $db == null || ! is_object ( $db )) {
2007-10-27 01:32:17 +00:00
return $this -> cakeError ( 'missingConnection' , array ( array ( 'className' => $this -> alias )));
2006-07-30 12:24:03 +00:00
}
}
/**
2006-08-25 00:43:48 +00:00
* Gets the DataSource to which this model is bound .
* Not safe for use with some versions of PHP4 , because this class is overloaded .
2006-07-30 12:24:03 +00:00
*
2007-11-05 03:36:12 +00:00
* @ return object A DataSource object
* @ access public
2006-07-30 12:24:03 +00:00
*/
function & getDataSource () {
2006-08-25 00:43:48 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
return $db ;
2006-07-30 12:24:03 +00:00
}
/**
* Gets all the models with which this model is associated
*
2007-11-05 03:36:12 +00:00
* @ param string $type Only result associations of this type
* @ return array Associations
* @ access public
2006-07-30 12:24:03 +00:00
*/
function getAssociated ( $type = null ) {
if ( $type == null ) {
$associated = array ();
foreach ( $this -> __associations as $assoc ) {
if ( ! empty ( $this -> { $assoc })) {
$models = array_keys ( $this -> { $assoc });
foreach ( $models as $m ) {
$associated [ $m ] = $assoc ;
}
}
}
return $associated ;
} elseif ( in_array ( $type , $this -> __associations )) {
if ( empty ( $this -> { $type })) {
return array ();
}
return array_keys ( $this -> { $type });
} else {
2007-12-08 06:08:03 +00:00
$assoc = array_merge ( $this -> hasOne , $this -> hasMany , $this -> belongsTo , $this -> hasAndBelongsToMany );
2006-07-30 12:24:03 +00:00
if ( array_key_exists ( $type , $assoc )) {
2007-01-16 12:53:39 +00:00
foreach ( $this -> __associations as $a ) {
if ( isset ( $this -> { $a }[ $type ])) {
$assoc [ $type ][ 'association' ] = $a ;
break ;
}
}
2006-07-30 12:24:03 +00:00
return $assoc [ $type ];
}
return null ;
}
}
2008-01-01 23:57:17 +00:00
/**
* Gets the name and fields to be used by a join model . This allows specifying join fields in the association definition .
*
* @ param object $model The model to be joined
* @ param mixed $with The 'with' key of the model association
* @ param array $keys Any join keys which must be merged with the keys queried
* @ return array
*/
function joinModel ( $assoc , $keys = array ()) {
if ( is_string ( $assoc )) {
return array ( $assoc , array_keys ( $this -> { $assoc } -> schema ()));
} elseif ( is_array ( $assoc )) {
$with = key ( $assoc );
return array ( $with , array_unique ( array_merge ( $assoc [ $with ], $keys )));
} else {
trigger_error ( sprintf ( __ ( 'Invalid join model settings in %s' , true ), $model -> alias ), E_USER_WARNING );
}
}
2006-07-30 12:24:03 +00:00
/**
* Before find callback
*
* @ param array $queryData Data used to execute this query , i . e . conditions , order , etc .
2008-01-01 18:34:55 +00:00
* @ return mixed true if the operation should continue , false if it should abort ; or , modified $queryData to continue with new $queryData
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2006-12-23 20:36:00 +00:00
function beforeFind ( $queryData ) {
2006-07-30 12:24:03 +00:00
return true ;
}
/**
2008-02-28 01:50:38 +00:00
* After find callback . Can be used to modify any results returned by find () .
2006-07-30 12:24:03 +00:00
*
* @ param mixed $results The results of the find operation
2007-10-22 16:54:36 +00:00
* @ param boolean $primary Whether this model is being queried directly ( vs . being queried as an association )
2006-07-30 12:24:03 +00:00
* @ return mixed Result of the find operation
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2006-08-18 08:00:54 +00:00
function afterFind ( $results , $primary = false ) {
2006-07-30 12:24:03 +00:00
return $results ;
}
/**
* Before save callback
*
2007-10-22 16:09:35 +00:00
* @ return boolean True if the operation should continue , false if it should abort
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function beforeSave () {
return true ;
}
/**
* After save callback
*
2007-10-22 16:54:36 +00:00
* @ param boolean $created True if this save created a new record
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-02-07 00:36:19 +00:00
function afterSave ( $created ) {
2006-07-30 12:24:03 +00:00
}
/**
* Before delete callback
*
2007-11-25 05:14:07 +00:00
* @ param boolean $cascade If true records that depend on this record will also be deleted
2007-10-22 16:09:35 +00:00
* @ return boolean True if the operation should continue , false if it should abort
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
2007-11-25 05:14:07 +00:00
function beforeDelete ( $cascade = true ) {
2006-07-30 12:24:03 +00:00
return true ;
}
/**
* After delete callback
*
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function afterDelete () {
}
/**
* Before validate callback
*
2007-11-05 03:36:12 +00:00
* @ return boolean True if validate operation should continue , false to abort
* @ access public
2006-07-30 12:24:03 +00:00
*/
function beforeValidate () {
return true ;
}
/**
* DataSource error callback
*
2007-11-05 03:36:12 +00:00
* @ access public
2006-07-30 12:24:03 +00:00
*/
function onError () {
}
/**
2007-11-05 03:36:12 +00:00
* Private method . Clears cache for this model
2006-01-17 17:52:23 +00:00
*
2007-10-01 16:49:37 +00:00
* @ param string $type If null this deletes cached views if Cache . check is true
2006-07-30 12:24:03 +00:00
* Will be used to allow deleting query cache also
2007-10-22 16:09:35 +00:00
* @ return boolean true on delete
2007-11-05 03:36:12 +00:00
* @ access protected
* @ todo
2006-01-17 17:52:23 +00:00
*/
2006-07-30 12:24:03 +00:00
function _clearCache ( $type = null ) {
if ( $type === null ) {
2007-10-01 16:49:37 +00:00
if ( Configure :: read ( 'Cache.check' ) === true ) {
2007-10-27 01:32:17 +00:00
$assoc [] = strtolower ( Inflector :: pluralize ( $this -> alias ));
2007-06-20 06:15:35 +00:00
foreach ( $this -> __associations as $key => $association ) {
foreach ( $this -> $association as $key => $className ) {
2006-07-30 12:24:03 +00:00
$check = strtolower ( Inflector :: pluralize ( $className [ 'className' ]));
if ( ! in_array ( $check , $assoc )) {
$assoc [] = strtolower ( Inflector :: pluralize ( $className [ 'className' ]));
}
}
}
clearCache ( $assoc );
return true ;
}
} else {
//Will use for query cache deleting
}
}
/**
* Called when serializing a model
*
2007-11-05 03:36:12 +00:00
* @ return array Set of object variable names this model has
* @ access private
2006-07-30 12:24:03 +00:00
*/
function __sleep () {
$return = array_keys ( get_object_vars ( $this ));
return $return ;
2006-07-22 14:13:07 +00:00
}
2006-10-28 00:42:51 +00:00
/**
* Called when unserializing a model
*
2007-11-05 03:36:12 +00:00
* @ access private
2006-10-28 00:42:51 +00:00
*/
function __wakeup () {
}
2006-01-12 02:10:47 +00:00
}
2007-01-27 17:45:26 +00:00
if ( ! defined ( 'CAKEPHP_UNIT_TEST_EXECUTION' )) {
Overloadable :: overload ( 'Model' );
}
2008-05-15 15:37:55 +00:00
?>