2006-01-12 02:10:47 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object-relational mapper.
|
|
|
|
*
|
|
|
|
* DBO-backed object data model, for mapping database tables to Cake objects.
|
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* PHP versions 4
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-01-20 07:46:14 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
2006-01-12 02:10:47 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2006-01-20 07:46:14 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
2006-01-12 02:10:47 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
|
|
|
* @since CakePHP v 0.10.0.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Included libs
|
|
|
|
*/
|
|
|
|
uses('class_registry', 'validators');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Model extends Object
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the DataSource connection that this Model uses
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $useDbConfig = 'default';
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The DataSource connection object that this Model uses
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $db = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here... Still used?
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
2006-01-20 07:46:14 +00:00
|
|
|
* @todo Is this still used? -OJ 22 nov 2006
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $parent = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Custom database table name.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $useTable = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Custom display field name. Display fields are used by Scaffold, in SELECT boxes' OPTION elements.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $displayField = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*Value of the primary key ID of the record that this model is currently pointing to
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $id = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for the data that this model gets from persistent storage (the database).
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $data = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table name for this Model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $table = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the ID field for this Model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $primaryKey = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table metadata
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $_tableInfo = null;
|
2006-01-12 02:10:47 +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
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $validate = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Errors in validation
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $validationErrors = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Database table prefix for tables in model.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $tablePrefix = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $name = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Name of the current model.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $currentModel = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of table names included in the Model description. Used for associations.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $tableToModel = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of Model names by used tables. Used for associations.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $modelToTable = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-01-22 05:12:28 +00:00
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* List of Foreign Key names to used tables. Used for associations.
|
2006-01-22 05:12:28 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $keyToTable = array();
|
2006-01-22 05:12:28 +00:00
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Alias table names for model, for use in SQL JOIN statements.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $alias = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not transactions for this model should be logged
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $logTransactions = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Whether or not to enable transactions for this model (i.e. BEGIN/COMMIT/ROLLBACK)
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $transactional = false;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* belongsTo association
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $belongsTo = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hasOne association
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $hasOne = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hasMany association
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $hasMany = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hasAndBelongsToMany association
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $hasAndBelongsToMany = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-01-18 06:46:28 +00:00
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Depth of recursive association
|
2006-01-18 06:46:28 +00:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $recursive = 1;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default association keys
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $__associationKeys = array('belongsTo' => array('className', 'conditions', 'order', 'foreignKey', 'counterCache'),
|
2006-01-12 02:10:47 +00:00
|
|
|
'hasOne' => array('className', 'conditions', 'order', 'foreignKey', 'dependent'),
|
2006-02-23 11:40:52 +00:00
|
|
|
'hasMany' => array('className', 'conditions', 'order', 'foreignKey', 'fields', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'),
|
2006-01-12 02:10:47 +00:00
|
|
|
'hasAndBelongsToMany' => array('className', 'joinTable', 'fields', 'foreignKey', 'associationForeignKey', 'conditions', 'order', 'uniq', 'finderQuery', 'deleteQuery', 'insertQuery')
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds provided/generated association key names and other data for all associations
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $__associations = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The last inserted ID of the data that this model created
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $__insertID = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of records returned by the last query
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $__numRows = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of records affected by the last query
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $__affectedRows = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. Binds the Model's database table to the object.
|
|
|
|
*
|
|
|
|
* @param integer $id
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param string $table Name of database table to use.
|
|
|
|
* @param DataSource $ds DataSource connection object.
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
|
|
|
function __construct ($id=false, $table=null, $ds=null)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
if ($this->name === null)
|
|
|
|
{
|
|
|
|
$this->name = get_class($this);
|
|
|
|
}
|
|
|
|
if ($this->primaryKey === null)
|
|
|
|
{
|
|
|
|
$this->primaryKey = 'id';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->currentModel = Inflector::underscore($this->name);
|
|
|
|
|
|
|
|
ClassRegistry::addObject($this->currentModel, $this);
|
|
|
|
$this->id = $id;
|
|
|
|
|
|
|
|
if($this->useTable !== false)
|
|
|
|
{
|
2006-01-22 05:12:28 +00:00
|
|
|
$this->setDataSource($ds);
|
|
|
|
if ($table)
|
|
|
|
{
|
|
|
|
$tableName = $table;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($this->useTable)
|
|
|
|
{
|
|
|
|
$tableName = $this->useTable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$tableName = Inflector::tableize($this->name);
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
if (in_array('settableprefix', get_class_methods($this)))
|
|
|
|
{
|
|
|
|
$this->setTablePrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->tablePrefix)
|
|
|
|
{
|
|
|
|
$this->setSource($this->tablePrefix.$tableName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->setSource($tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->__createLinks();
|
|
|
|
|
|
|
|
if ($this->displayField == null)
|
|
|
|
{
|
|
|
|
if ($this->hasField('title'))
|
|
|
|
{
|
|
|
|
$this->displayField = 'title';
|
|
|
|
}
|
|
|
|
if ($this->hasField('name'))
|
|
|
|
{
|
|
|
|
$this->displayField = 'name';
|
|
|
|
}
|
|
|
|
if ($this->displayField == null)
|
|
|
|
{
|
|
|
|
$this->displayField = $this->primaryKey;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-01-17 17:52:23 +00:00
|
|
|
* PHP4 Only
|
|
|
|
*
|
2006-01-12 02:10:47 +00:00
|
|
|
* Handles custom method calls, like findBy<field> for DB models,
|
2006-02-20 05:09:48 +00:00
|
|
|
* and custom RPC calls for remote data sources
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $method
|
|
|
|
* @param unknown_type $params
|
|
|
|
* @param unknown_type $return
|
|
|
|
* @return unknown
|
2006-01-12 02:10:47 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
function __call($method, $params, &$return)
|
|
|
|
{
|
2006-02-20 05:09:48 +00:00
|
|
|
$return = $this->db->query($method, $params, $this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind model associations on the fly.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return true
|
|
|
|
*/
|
|
|
|
function bindModel($params)
|
|
|
|
{
|
|
|
|
foreach($params as $assoc => $model)
|
2006-02-20 03:03:05 +00:00
|
|
|
{
|
2006-02-22 09:15:12 +00:00
|
|
|
$this->__backAssociation[$assoc] = $this->{$assoc};
|
|
|
|
foreach($model as $key => $value)
|
|
|
|
{
|
|
|
|
$modelName = $key;
|
|
|
|
$this->__constructLinkedModel($modelName, $modelName);
|
|
|
|
$this->{$assoc}[$modelName] = $model[$modelName];
|
|
|
|
$this->__generateAssociation($assoc, $modelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn off associations on the fly.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return true
|
|
|
|
*/
|
|
|
|
function unbindModel($params)
|
|
|
|
{
|
|
|
|
foreach($params as $assoc => $models)
|
|
|
|
{
|
|
|
|
$this->__backAssociation[$assoc] = $this->{$assoc};
|
|
|
|
foreach($models as $model)
|
|
|
|
{
|
2006-03-08 03:13:32 +00:00
|
|
|
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$assoc});
|
2006-02-22 09:15:12 +00:00
|
|
|
unset($this->{$assoc}[$model]);
|
|
|
|
}
|
2006-02-20 03:03:05 +00:00
|
|
|
}
|
2006-02-20 05:09:48 +00:00
|
|
|
return true;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private helper method to create a set of associations.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __createLinks()
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
// Convert all string-based associations to array based
|
2006-01-12 02:10:47 +00:00
|
|
|
foreach($this->__associations as $type)
|
|
|
|
{
|
|
|
|
if(!is_array($this->{$type}))
|
|
|
|
{
|
|
|
|
$this->{$type} = explode(',', $this->{$type});
|
|
|
|
foreach ($this->{$type} as $i => $className)
|
|
|
|
{
|
|
|
|
$className = trim($className);
|
|
|
|
unset($this->{$type}[$i]);
|
|
|
|
$this->{$type}[$className] = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->{$type} as $assoc => $value)
|
|
|
|
{
|
|
|
|
$className = $assoc;
|
|
|
|
if (isset($value['className']) && $value['className'] !== null)
|
|
|
|
{
|
|
|
|
$className = $value['className'];
|
|
|
|
}
|
2006-02-20 03:03:05 +00:00
|
|
|
$this->__constructLinkedModel($assoc, $className);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->__associations as $type)
|
|
|
|
{
|
|
|
|
foreach ($this->{$type} as $assoc => $value)
|
|
|
|
{
|
|
|
|
$this->__generateAssociation($type, $assoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private helper method to create associated models of given class.
|
|
|
|
* @param string $assoc
|
|
|
|
* @param string $className Class name
|
|
|
|
* @param string $type Type of assocation
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-20 03:03:05 +00:00
|
|
|
function __constructLinkedModel($assoc, $className)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$colKey = Inflector::underscore($className);
|
|
|
|
if(ClassRegistry::isKeySet($colKey))
|
|
|
|
{
|
2006-02-19 01:54:39 +00:00
|
|
|
$this->{$className} =& ClassRegistry::getObject($colKey);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-20 18:43:05 +00:00
|
|
|
$this->{$className} =& new $className();
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
2006-01-12 03:56:59 +00:00
|
|
|
$this->alias[$assoc] = $this->{$className}->table;
|
|
|
|
$this->tableToModel[$this->{$className}->table] = $className;
|
|
|
|
$this->modelToTable[$className] = $this->{$className}->table;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Build array-based association from string.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param string $type "Belongs", "One", "Many", "ManyTo"
|
|
|
|
* @param string $assoc
|
2006-02-17 10:12:15 +00:00
|
|
|
* @todo Is the second parameter in use at the moment? It is not referred to in the method OJ, 30. jan 2006
|
2006-01-17 17:52:23 +00:00
|
|
|
* @access private
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
|
|
|
function __generateAssociation ($type, $assoc)
|
|
|
|
{
|
|
|
|
foreach ($this->{$type} as $assocKey => $assocData)
|
|
|
|
{
|
|
|
|
$class = $assocKey;
|
|
|
|
if (isset($this->{$type}[$assocKey]['className']) && $this->{$type}[$assocKey]['className'] !== null)
|
|
|
|
{
|
|
|
|
$class = $this->{$type}[$assocKey]['className'];
|
|
|
|
}
|
|
|
|
foreach($this->__associationKeys[$type] as $key)
|
|
|
|
{
|
|
|
|
if (!isset($this->{$type}[$assocKey][$key]) || $this->{$type}[$assocKey][$key] == null)
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
switch($key)
|
|
|
|
{
|
|
|
|
case 'fields':
|
2006-01-15 12:18:57 +00:00
|
|
|
$data = '';
|
2006-01-12 02:10:47 +00:00
|
|
|
break;
|
|
|
|
case 'foreignKey':
|
2006-01-12 03:56:59 +00:00
|
|
|
$data = Inflector::singularize($this->table).'_id';
|
2006-01-12 02:10:47 +00:00
|
|
|
if ($type == 'belongsTo')
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$data = Inflector::singularize($this->{$class}->table).'_id';
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'associationForeignKey':
|
2006-01-12 03:56:59 +00:00
|
|
|
$data = Inflector::singularize($this->{$class}->table) . '_id';
|
2006-01-12 02:10:47 +00:00
|
|
|
break;
|
|
|
|
case 'joinTable':
|
2006-01-12 03:56:59 +00:00
|
|
|
$tables = array($this->table, $this->{$class}->table);
|
2006-01-12 02:10:47 +00:00
|
|
|
sort($tables);
|
|
|
|
$data = $tables[0].'_'.$tables[1];
|
|
|
|
break;
|
|
|
|
case 'className':
|
|
|
|
$data = $class;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->{$type}[$assocKey][$key] = $data;
|
|
|
|
}
|
2006-01-22 05:12:28 +00:00
|
|
|
if($key == 'foreignKey' && !isset($this->keyToTable[$this->{$type}[$assocKey][$key]]))
|
|
|
|
{
|
|
|
|
$this->keyToTable[$this->{$type}[$assocKey][$key]][0] = $this->{$class}->table;
|
|
|
|
$this->keyToTable[$this->{$type}[$assocKey][$key]][1] = $this->{$class}->name;
|
|
|
|
}
|
2006-01-12 02:10:47 +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
|
|
|
|
*/
|
|
|
|
function setSource($tableName)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
if($this->db->isInterfaceSupported('listSources'))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-03-08 03:13:32 +00:00
|
|
|
if (!in_array(low($tableName), $this->db->listSources()) && !in_array($tableName, $this->db->listSources()))
|
2006-02-18 23:42:21 +00:00
|
|
|
{
|
|
|
|
return $this->cakeError('missingTable',array(array('className' => $this->name,
|
2006-01-12 03:56:59 +00:00
|
|
|
'table' => $tableName)));
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->table = $tableName;
|
|
|
|
$this->tableToModel[$this->table] = $this->name;
|
|
|
|
$this->loadInfo();
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
$this->table = $tableName;
|
|
|
|
$this->tableToModel[$this->table] = $this->name;
|
|
|
|
$this->loadInfo();
|
2006-01-12 02:10:47 +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
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function set ($one, $two=null)
|
|
|
|
{
|
|
|
|
$this->validationErrors = null;
|
|
|
|
if (is_array($one))
|
|
|
|
{
|
|
|
|
$data = $one;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data = array($one=>$two);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($data as $n => $v)
|
|
|
|
{
|
|
|
|
foreach ($v as $x => $y)
|
|
|
|
{
|
|
|
|
if($x == $this->primaryKey)
|
|
|
|
{
|
|
|
|
$this->id = $y;
|
|
|
|
}
|
|
|
|
$this->data[$n][$x] = $y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of table metadata (column names and types) from the database.
|
|
|
|
*
|
|
|
|
* @return array Array of table metadata
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function loadInfo ()
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
if (!is_object($this->_tableInfo) && $this->db->isInterfaceSupported('describe'))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$this->_tableInfo = new NeatArray($this->db->describe($this));
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
return $this->_tableInfo;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-02-16 09:29:28 +00:00
|
|
|
/**
|
|
|
|
* Returns an associative array of field names and column types.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getColumnTypes ()
|
|
|
|
{
|
|
|
|
$columns = $this->loadInfo();
|
|
|
|
$columns = $columns->value;
|
2006-02-16 09:29:28 +00:00
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
$cols = array();
|
|
|
|
foreach($columns as $col) {
|
2006-02-28 07:35:01 +00:00
|
|
|
$cols[$col['name']] = $this->db->column($col['type']);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
|
|
|
return $cols;
|
|
|
|
}
|
2006-02-16 09:29:28 +00:00
|
|
|
|
2006-03-01 19:13:48 +00:00
|
|
|
/**
|
|
|
|
* Returns the column type of a column in the model
|
|
|
|
*
|
|
|
|
* @param string $column The name of the model column
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getColumnType ($column)
|
|
|
|
{
|
|
|
|
$columns = $this->loadInfo();
|
|
|
|
$columns = $columns->value;
|
|
|
|
|
|
|
|
$cols = array();
|
|
|
|
foreach($columns as $col) {
|
|
|
|
if ($col['name'] == $column)
|
|
|
|
{
|
|
|
|
return $this->db->column($col['type']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns true if this Model has given field in its database table.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param string $name Name of field to look for
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function hasField ($name)
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
if (empty($this->_tableInfo))
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
$this->loadInfo();
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
|
|
|
if($this->_tableInfo != null)
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
return $this->_tableInfo->findIn('name', $name);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
|
|
|
return null;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Initializes the model for writing a new record.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @return boolean True
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function create ()
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
$this->id = false;
|
|
|
|
unset($this->data);
|
|
|
|
$this->data = array();
|
|
|
|
return true;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated
|
|
|
|
*
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function setId ($id)
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
$this->id = $id;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Deprecated. Use query() instead.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function findBySql ($sql)
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
return $this->query($sql);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of fields from the database
|
|
|
|
*
|
|
|
|
* @param mixed $id The ID of the record to read
|
|
|
|
* @param mixed $fields String of single fieldname, or an array of fieldnames.
|
|
|
|
* @return array Array of database fields
|
|
|
|
*/
|
2006-01-17 05:13:38 +00:00
|
|
|
function read ($fields = null, $id = null)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$this->validationErrors = null;
|
|
|
|
if ($id != null)
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->id;
|
|
|
|
if (is_array($this->id))
|
|
|
|
{
|
|
|
|
$id = $this->id[0];
|
|
|
|
}
|
|
|
|
|
2006-02-01 13:26:23 +00:00
|
|
|
if ($this->id !== null && $this->id !== false)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$field = $this->db->name($this->name).'.'.$this->db->name($this->primaryKey);
|
2006-03-01 19:13:48 +00:00
|
|
|
return $this->find($field . ' = ' . $this->db->value($id, $this->getColumnType($this->primaryKey)), $fields);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns contents of a field in a query matching given conditions.
|
|
|
|
*
|
|
|
|
* @param string $name Name of field to get
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param array $conditions SQL conditions (defaults to NULL)
|
|
|
|
* @param string $order SQL ORDER BY fragment
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return field contents
|
|
|
|
*/
|
|
|
|
function field ($name, $conditions = null, $order = null)
|
|
|
|
{
|
|
|
|
if (isset($this->data[$this->name][$name]))
|
|
|
|
{
|
|
|
|
return $this->data[$this->name][$name];
|
|
|
|
}
|
|
|
|
|
2006-03-02 06:23:16 +00:00
|
|
|
if ($data = $this->find($conditions, $name, $order, 0))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
if (isset($data[$this->name][$name]))
|
|
|
|
{
|
|
|
|
return $data[$this->name][$name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* @param boolean $validate Whether or not this model should validate before saving (defaults to false)
|
|
|
|
* @return boolean True on success save
|
|
|
|
*/
|
|
|
|
function saveField($name, $value, $validate = false)
|
|
|
|
{
|
|
|
|
return $this->save(array($this->name => array($name => $value)), $validate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves model data to the database.
|
2006-02-17 10:12:15 +00:00
|
|
|
* By default, validation occurs before save.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param array $data Data to save.
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param boolean $validate If set, validation will be done before the save
|
|
|
|
* @param array $fieldList List of fields to allow to be written
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return boolean success
|
|
|
|
*/
|
2006-02-17 10:12:15 +00:00
|
|
|
function save ($data = null, $validate = true, $fieldList = array())
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
if ($data)
|
|
|
|
{
|
2006-02-02 13:51:43 +00:00
|
|
|
if (countdim($data) == 1)
|
2006-02-01 13:26:23 +00:00
|
|
|
{
|
|
|
|
$this->set(array($this->name => $data));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->set($data);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-02-01 13:26:23 +00:00
|
|
|
|
2006-02-17 10:12:15 +00:00
|
|
|
$whitelist = !(empty($fieldList) || count($fieldList) == 0);
|
|
|
|
|
2006-03-12 00:11:40 +00:00
|
|
|
if(!$this->beforeValidate())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-17 10:12:15 +00:00
|
|
|
if ($validate && !$this->validates())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->beforeSave())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = $values = array();
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
if(count($this->data) > 1)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$weHaveMulti = true;
|
|
|
|
$joined = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$weHaveMulti = false;
|
|
|
|
}
|
|
|
|
|
2006-03-08 03:13:32 +00:00
|
|
|
$newID = null;
|
2006-02-17 10:12:15 +00:00
|
|
|
foreach ($this->data as $n => $v)
|
|
|
|
{
|
|
|
|
if(isset($weHaveMulti) && $count > 0 && count($this->hasAndBelongsToMany) > 0)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-17 10:12:15 +00:00
|
|
|
$joined[] = $v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach ($v as $x => $y)
|
|
|
|
{
|
|
|
|
if ($this->hasField($x) && ($whitelist && in_array($x, $fieldList) || !$whitelist))
|
|
|
|
{
|
|
|
|
$fields[] = $x;
|
|
|
|
$values[] = $y;
|
|
|
|
|
|
|
|
if($x == $this->primaryKey && !is_numeric($y))
|
|
|
|
{
|
|
|
|
$newID = $y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->id) && $this->hasField('created') && !in_array('created', $fields) && ($whitelist && in_array('created', $fieldList) || !$whitelist))
|
|
|
|
{
|
|
|
|
$fields[] = 'created';
|
|
|
|
$values[] = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
if ($this->hasField('modified') && !in_array('modified', $fields) && ($whitelist && in_array('modified', $fieldList) || !$whitelist))
|
|
|
|
{
|
|
|
|
$fields[] = 'modified';
|
|
|
|
$values[] = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
if ($this->hasField('updated') && !in_array('updated', $fields) && ($whitelist && in_array('updated', $fieldList) || !$whitelist))
|
|
|
|
{
|
|
|
|
$fields[] = 'updated';
|
|
|
|
$values[] = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->exists())
|
|
|
|
{
|
|
|
|
$this->id = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($fields))
|
|
|
|
{
|
|
|
|
if(!empty($this->id))
|
|
|
|
{
|
|
|
|
if ($this->db->update($this, $fields, $values))
|
|
|
|
{
|
|
|
|
if(!empty($joined))
|
|
|
|
{
|
|
|
|
$this->__saveMulti($joined, $this->id);
|
|
|
|
}
|
|
|
|
$this->afterSave();
|
|
|
|
$this->data = false;
|
2006-03-12 06:58:53 +00:00
|
|
|
$this->_clearCache();
|
2006-02-17 10:12:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-28 07:35:01 +00:00
|
|
|
return false;
|
2006-02-17 10:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if($this->db->create($this, $fields, $values))
|
|
|
|
{
|
|
|
|
$this->__insertID = $this->db->lastInsertId($this->table, $this->primaryKey);
|
|
|
|
|
2006-03-08 03:13:32 +00:00
|
|
|
if (!$this->__insertID && $newID != null)
|
2006-02-17 10:12:15 +00:00
|
|
|
{
|
|
|
|
$this->__insertID = $newID;
|
|
|
|
$this->id = $newID;
|
|
|
|
}
|
2006-03-08 03:13:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->id = $this->__insertID;
|
|
|
|
}
|
2006-02-17 10:12:15 +00:00
|
|
|
|
|
|
|
if(!empty($joined))
|
|
|
|
{
|
|
|
|
$this->__saveMulti($joined, $this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->afterSave();
|
|
|
|
$this->data = false;
|
2006-03-12 06:58:53 +00:00
|
|
|
$this->_clearCache();
|
2006-02-17 10:12:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves model hasAndBelongsToMany data to the database.
|
|
|
|
*
|
|
|
|
* @param array $joined Data to save.
|
|
|
|
* @param string $id
|
|
|
|
* @return
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __saveMulti ($joined, $id)
|
|
|
|
{
|
|
|
|
foreach ($joined as $x => $y)
|
|
|
|
{
|
|
|
|
foreach ($y as $assoc => $value)
|
|
|
|
{
|
|
|
|
$joinTable[] = $this->hasAndBelongsToMany[$assoc]['joinTable'];
|
|
|
|
$mainKey = $this->hasAndBelongsToMany[$assoc]['foreignKey'];
|
|
|
|
$keys[] = $mainKey;
|
|
|
|
$keys[] = $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
|
|
|
|
$fields[] = join(',', $keys);
|
|
|
|
unset($keys);
|
|
|
|
|
|
|
|
foreach ($value as $update)
|
|
|
|
{
|
|
|
|
if(!empty($update))
|
|
|
|
{
|
2006-03-01 19:13:48 +00:00
|
|
|
$values[] = $this->db->value($id, $this->getColumnType($this->primaryKey));
|
2006-01-12 03:56:59 +00:00
|
|
|
$values[] = $this->db->value($update);
|
2006-01-12 02:10:47 +00:00
|
|
|
$values = join(',', $values);
|
2006-01-12 16:55:46 +00:00
|
|
|
$newValues[] = "({$values})";
|
2006-01-12 02:10:47 +00:00
|
|
|
unset($values);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!empty($newValues))
|
|
|
|
{
|
|
|
|
$newValue[] = join(',', $newValues);
|
|
|
|
unset($newValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$total = count($joinTable);
|
|
|
|
for ($count = 0; $count < $total; $count++)
|
|
|
|
{
|
2006-01-12 16:55:46 +00:00
|
|
|
$this->db->execute("DELETE FROM {$joinTable[$count]} WHERE $mainKey = '{$id}'");
|
2006-01-12 02:10:47 +00:00
|
|
|
if(!empty($newValue[$count]))
|
|
|
|
{
|
2006-01-12 16:55:46 +00:00
|
|
|
$this->db->execute("INSERT INTO {$joinTable[$count]} ({$fields[$count]}) VALUES {$newValue[$count]}");
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synonym for del().
|
|
|
|
*
|
|
|
|
* @param mixed $id
|
|
|
|
* @see function del
|
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
|
|
|
function remove ($id=null)
|
|
|
|
{
|
|
|
|
return $this->del($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes record for given id. If no id is given, the current id is used. Returns true on success.
|
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param mixed $id Id of record to delete
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
2006-02-19 01:54:39 +00:00
|
|
|
function del ($id = null, $cascade = true)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
if ($id)
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
2006-02-19 01:54:39 +00:00
|
|
|
$id = $this->id;
|
2006-01-12 02:10:47 +00:00
|
|
|
if($this->beforeDelete())
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
if ($this->id && $this->db->delete($this))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-02-19 01:54:39 +00:00
|
|
|
$this->__deleteMulti($id);
|
|
|
|
$this->__deleteHasMany($id, $cascade);
|
|
|
|
$this->__deleteHasOne($id, $cascade);
|
2006-01-12 02:10:47 +00:00
|
|
|
$this->afterDelete();
|
2006-03-12 06:58:53 +00:00
|
|
|
$this->_clearCache();
|
2006-01-12 02:10:47 +00:00
|
|
|
$this->id = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-22 09:15:12 +00:00
|
|
|
/**
|
|
|
|
* Alias for del()
|
|
|
|
*
|
|
|
|
* @param mixed $id Id of record to delete
|
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
|
|
|
function delete ($id = null, $cascade = true)
|
|
|
|
{
|
|
|
|
return $this->del($id, $cascade);
|
|
|
|
}
|
|
|
|
|
2006-02-17 10:12:15 +00:00
|
|
|
/**
|
|
|
|
* Cascades model deletes to hasMany relationships.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return null
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-19 01:54:39 +00:00
|
|
|
function __deleteHasMany ($id, $cascade)
|
2006-02-17 10:12:15 +00:00
|
|
|
{
|
|
|
|
foreach ($this->hasMany as $assoc => $data)
|
|
|
|
{
|
2006-02-19 01:54:39 +00:00
|
|
|
if($data['dependent'] === true && $cascade === true)
|
|
|
|
{
|
|
|
|
$model =& $this->{$data['className']};
|
|
|
|
$field = $model->escapeField($data['foreignKey']);
|
|
|
|
$model->recursive = 0;
|
2006-02-22 09:15:12 +00:00
|
|
|
$records = $model->findAll("$field = '$id'", $model->primaryKey, null, null);
|
2006-02-17 10:12:15 +00:00
|
|
|
|
2006-02-19 01:54:39 +00:00
|
|
|
foreach($records as $record)
|
|
|
|
{
|
2006-02-22 09:15:12 +00:00
|
|
|
$model->del($record[$data['className']][$model->primaryKey]);
|
2006-02-19 01:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades model deletes to hasOne relationships.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return null
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __deleteHasOne ($id, $cascade)
|
|
|
|
{
|
|
|
|
foreach ($this->hasOne as $assoc => $data)
|
|
|
|
{
|
|
|
|
if($data['dependent'] === true && $cascade === true)
|
2006-02-17 10:12:15 +00:00
|
|
|
{
|
2006-02-19 01:54:39 +00:00
|
|
|
$model =& $this->{$data['className']};
|
|
|
|
$field = $model->escapeField($data['foreignKey']);
|
|
|
|
$model->recursive = 0;
|
2006-02-22 09:15:12 +00:00
|
|
|
$records = $model->findAll("$field = '$id'", $model->primaryKey, null, null);
|
2006-02-19 01:54:39 +00:00
|
|
|
|
|
|
|
foreach($records as $record)
|
|
|
|
{
|
2006-02-22 09:15:12 +00:00
|
|
|
$model->del($record[$data['className']][$model->primaryKey]);
|
2006-02-19 01:54:39 +00:00
|
|
|
}
|
2006-02-17 10:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades model deletes to HABTM join keys.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return null
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-19 01:54:39 +00:00
|
|
|
function __deleteMulti ($id)
|
2006-02-17 10:12:15 +00:00
|
|
|
{
|
|
|
|
foreach ($this->hasAndBelongsToMany as $assoc => $data)
|
|
|
|
{
|
2006-02-19 03:56:54 +00:00
|
|
|
$this->db->execute("DELETE FROM ".$this->db->name($data['joinTable'])." WHERE ".$this->db->name($data['foreignKey'])." = '{$id}'");
|
2006-02-17 10:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a record with set id exists.
|
|
|
|
*
|
|
|
|
* @return boolean True if such a record exists
|
|
|
|
*/
|
|
|
|
function exists ()
|
|
|
|
{
|
|
|
|
if ($this->id)
|
|
|
|
{
|
|
|
|
$id = $this->id;
|
2006-01-12 22:06:11 +00:00
|
|
|
if (is_array($id))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$id = $id[0];
|
|
|
|
}
|
2006-03-01 19:13:48 +00:00
|
|
|
return $this->db->hasAny($this->table,$this->primaryKey.'='.$this->db->value($id, $this->getColumnType($this->primaryKey)));
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a record that meets given conditions exists
|
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param array $conditions SQL conditions array
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return boolean True if such a record exists
|
|
|
|
*/
|
|
|
|
function hasAny ($conditions = null)
|
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
return ($this->findCount($conditions) != false);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single row as a resultset array.
|
2006-02-18 23:42:21 +00:00
|
|
|
* By using the $recursive parameter, the call can access further "levels of association" than
|
2006-02-17 10:12:15 +00:00
|
|
|
* the ones this model is directly associated to.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param array $conditions SQL conditions array
|
2006-01-12 02:10:47 +00:00
|
|
|
* @param mixed $fields Either a single string of a field name, or an array of field names
|
|
|
|
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC")
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param int $recursive The number of levels deep to fetch associated records
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return array Array of records
|
|
|
|
*/
|
2006-01-18 06:46:28 +00:00
|
|
|
function find ($conditions = null, $fields = null, $order = null, $recursive = null)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-17 05:13:38 +00:00
|
|
|
$data = $this->findAll($conditions, $fields, $order, 1, null, $recursive);
|
2006-01-12 02:10:47 +00:00
|
|
|
if (empty($data[0]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a resultset array with specified fields from database matching given conditions.
|
2006-02-17 10:12:15 +00:00
|
|
|
* By using the $recursive parameter, the call can access further "levels of association" than
|
|
|
|
* the ones this model is directly associated to.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param mixed $conditions SQL conditions as a string or as an array('field'=>'value',...)
|
|
|
|
* @param mixed $fields Either a single string of a field name, or an array of field names
|
|
|
|
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC")
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param int $limit SQL LIMIT clause, for calculating items per page.
|
|
|
|
* @param int $page Page number, for accessing paged data
|
|
|
|
* @param int $recursive The number of levels deep to fetch associated records
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return array Array of records
|
|
|
|
*/
|
2006-03-12 08:56:11 +00:00
|
|
|
function findAll ($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
$this->id = $this->getID();
|
|
|
|
$offset = 0;
|
|
|
|
if ($page > 1)
|
|
|
|
{
|
|
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
}
|
|
|
|
$limit_str = '';
|
|
|
|
if ($limit)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$limit_str = $this->db->limit($limit, $offset);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$queryData = array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'fields' => $fields,
|
|
|
|
'joins' => array(),
|
|
|
|
'limit' => $limit_str,
|
|
|
|
'order' => $order
|
|
|
|
);
|
2006-03-12 00:11:40 +00:00
|
|
|
|
|
|
|
if (!$this->beforeFind($queryData))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-02-20 03:03:05 +00:00
|
|
|
$return = $this->afterFind($this->db->read($this, $queryData, $recursive));
|
|
|
|
|
|
|
|
if(isset($this->__backAssociation))
|
|
|
|
{
|
2006-02-20 08:45:44 +00:00
|
|
|
$this->__resetAssociations();
|
2006-02-20 03:03:05 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-02-20 08:45:44 +00:00
|
|
|
function __resetAssociations()
|
2006-02-20 03:03:05 +00:00
|
|
|
{
|
|
|
|
foreach ($this->__associations as $type)
|
|
|
|
{
|
|
|
|
if(isset($this->__backAssociation[$type]))
|
|
|
|
{
|
|
|
|
$this->{$type} = $this->__backAssociation[$type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($this->__backAssociation);
|
|
|
|
return true;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Runs a direct query against the bound DataSource, and returns the result.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param string $data Query data
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function execute ($data)
|
|
|
|
{
|
|
|
|
$data = $this->db->fetchAll($data);
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
{
|
|
|
|
foreach ($this->tableToModel as $key1 => $value1)
|
|
|
|
{
|
|
|
|
if (isset($data[$key][$key1]))
|
|
|
|
{
|
|
|
|
$newData[$key][$value1] = $data[$key][$key1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($newData))
|
|
|
|
{
|
|
|
|
return $newData;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of rows matching given SQL condition.
|
|
|
|
*
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param array $conditions SQL conditions array for findAll
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param int $recursize The number of levels deep to fetch associated records
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return int Number of matching rows
|
2006-02-17 10:12:15 +00:00
|
|
|
* @see Model::findAll
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function findCount ($conditions = null, $recursive = 0)
|
|
|
|
{
|
2006-01-17 05:13:38 +00:00
|
|
|
list($data) = $this->findAll($conditions, 'COUNT(*) AS count', null, null, 1, $recursive);
|
2006-01-12 02:10:47 +00:00
|
|
|
if (isset($data[0]['count']))
|
|
|
|
{
|
|
|
|
return $data[0]['count'];
|
|
|
|
}
|
|
|
|
return false;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Special findAll variation for tables joined to themselves.
|
2006-02-17 10:12:15 +00:00
|
|
|
* The table needs the fields id and parent_id to work.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param array $conditions Conditions for the findAll() call
|
|
|
|
* @param array $fields Fields for the findAll() call
|
|
|
|
* @param string $sort SQL ORDER BY statement
|
2006-02-17 10:12:15 +00:00
|
|
|
* @return array
|
2006-01-17 17:52:23 +00:00
|
|
|
* @todo Perhaps create a Component with this logic
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function findAllThreaded ($conditions=null, $fields=null, $sort=null)
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
return $this->__doThread(Model::findAll($conditions, $fields, $sort), null);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private, recursive helper method for findAllThreaded.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param string $root NULL or id for root node of operation
|
|
|
|
* @return array
|
|
|
|
* @access private
|
2006-02-17 10:12:15 +00:00
|
|
|
* @see findAllThreaded
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function __doThread ($data, $root)
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
$sizeOf = sizeof($data);
|
|
|
|
for ($ii=0; $ii < $sizeOf; $ii++)
|
|
|
|
{
|
2006-03-08 03:13:32 +00:00
|
|
|
if (($data[$ii][$this->name]['parent_id'] == $root) || (($root === null) && ($data[$ii][$this->name]['parent_id'] == '0')))
|
2006-02-18 23:42:21 +00:00
|
|
|
{
|
|
|
|
$tmp = $data[$ii];
|
|
|
|
if (isset($data[$ii][$this->name][$this->primaryKey]))
|
|
|
|
{
|
|
|
|
$tmp['children'] = $this->__doThread($data, $data[$ii][$this->name][$this->primaryKey]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$tmp['children'] = null;
|
|
|
|
}
|
|
|
|
$out[] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2006-02-17 10:12:15 +00:00
|
|
|
* @param string $field Field name (parameter for findAll)
|
2006-01-12 02:10:47 +00:00
|
|
|
* @param unknown_type $value
|
|
|
|
* @return array Array with keys "prev" and "next" that holds the id's
|
|
|
|
*/
|
2006-01-20 09:37:15 +00:00
|
|
|
function findNeighbours ($conditions = null, $field, $value)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
if(!is_null($conditions))
|
|
|
|
{
|
|
|
|
$conditions = $conditions.' AND ';
|
|
|
|
}
|
|
|
|
@list($prev) = Model::findAll($conditions. $field . ' < ' . $this->db->value($value), $field, $field . ' DESC', 1);
|
|
|
|
@list($next) = Model::findAll($conditions. $field . ' > ' . $this->db->value($value), $field, $field . ' ASC', 1);
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-01-12 16:55:46 +00:00
|
|
|
if (!isset($prev))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-12 16:55:46 +00:00
|
|
|
$prev = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-01-12 16:55:46 +00:00
|
|
|
if (!isset($next))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-12 16:55:46 +00:00
|
|
|
$next = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
return array('prev' => $prev, 'next' => $next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns a resultset for given SQL statement. Generic SQL queries should be made with this method.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return array Resultset
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function query ()
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
$params = func_get_args();
|
2006-01-12 03:56:59 +00:00
|
|
|
return call_user_func_array(array(&$this->db, 'query'), $params);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns true if all fields pass validation, otherwise false.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param array $data POST data
|
|
|
|
* @return boolean True if there are no errors
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function validates ($data = null)
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
if ($data == null)
|
|
|
|
{
|
|
|
|
$data = $this->data;
|
|
|
|
}
|
|
|
|
$errors = $this->invalidFields($data);
|
|
|
|
return count($errors) == 0;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of invalid fields.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array Array of invalid fields
|
|
|
|
*/
|
2006-03-14 02:26:08 +00:00
|
|
|
function invalidFields ($data = array())
|
2006-02-18 23:42:21 +00:00
|
|
|
{
|
2006-03-14 02:26:08 +00:00
|
|
|
if (!isset($this->validate))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-03-14 02:26:08 +00:00
|
|
|
if (is_array($this->validationErrors))
|
|
|
|
{
|
|
|
|
return $this->validationErrors;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-03-14 02:26:08 +00:00
|
|
|
if (empty($data) && isset($this->data))
|
|
|
|
{
|
|
|
|
$data = $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data[$this->name]))
|
|
|
|
{
|
|
|
|
$data = $data[$this->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validationErrors = array();
|
|
|
|
foreach ($this->validate as $field_name => $validator)
|
|
|
|
{
|
|
|
|
if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name]))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-03-14 02:26:08 +00:00
|
|
|
$this->validationErrors[$field_name] = 1;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-03-14 02:26:08 +00:00
|
|
|
}
|
|
|
|
return $this->validationErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a field as invalid
|
|
|
|
*
|
|
|
|
* @param string $field The name of the field to invalidate
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function invalidate ($field)
|
|
|
|
{
|
|
|
|
if (!is_array($this->validationErrors))
|
|
|
|
{
|
|
|
|
$this->validationErrors = array();
|
|
|
|
}
|
|
|
|
$this->validationErrors[$field] = 1;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns true if given field name is a foreign key in this Model.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param string $field Returns true if the input string ends in "_id"
|
|
|
|
* @return True if the field is a foreign key listed in the belongsTo array.
|
|
|
|
*/
|
|
|
|
function isForeignKey($field)
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
$foreignKeys = array();
|
|
|
|
if(count($this->belongsTo))
|
|
|
|
{
|
2006-01-12 02:10:47 +00:00
|
|
|
foreach ($this->belongsTo as $assoc => $data)
|
|
|
|
{
|
|
|
|
$foreignKeys[] = $data['foreignKey'];
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
return (bool)(in_array($field, $foreignKeys));
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the display field for this model
|
|
|
|
*
|
|
|
|
* @return string The name of the display field for this Model (i.e. 'name', 'title').
|
|
|
|
*/
|
|
|
|
function getDisplayField()
|
|
|
|
{
|
|
|
|
return $this->displayField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns a resultset array with specified fields from database matching given conditions. Method can be used to generate option lists for SELECT elements.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param mixed $conditions SQL conditions as a string or as an array('field'=>'value',...)
|
|
|
|
* @param mixed $fields Either a single string of a field name, or an array of field names
|
|
|
|
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC")
|
|
|
|
* @param int $limit SQL LIMIT clause, for calculating items per page
|
|
|
|
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
|
|
|
|
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
|
|
|
|
* @return array An associative array of records, where the id is the key, and the display field is the value
|
|
|
|
*/
|
|
|
|
function generateList ($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null)
|
|
|
|
{
|
2006-02-28 07:35:01 +00:00
|
|
|
if ($keyPath == null && $valuePath == null)
|
|
|
|
{
|
|
|
|
$fields = array($this->primaryKey, $this->displayField);
|
|
|
|
} else {
|
|
|
|
$fields = '*';
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
$result = $this->findAll($conditions, $fields, $order, $limit, 1, 0);
|
|
|
|
|
|
|
|
if ($keyPath == null)
|
|
|
|
{
|
|
|
|
$keyPath = '{n}.'.$this->name.'.'.$this->primaryKey;
|
|
|
|
}
|
|
|
|
if ($valuePath == null)
|
|
|
|
{
|
|
|
|
$valuePath = '{n}.'.$this->name.'.'.$this->displayField;
|
|
|
|
}
|
2006-01-12 03:56:59 +00:00
|
|
|
$keys = $this->db->getFieldValue($result, $keyPath);
|
|
|
|
$vals = $this->db->getFieldValue($result, $valuePath);
|
2006-01-12 02:10:47 +00:00
|
|
|
return array_combine($keys, $vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $field
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return string The name of the escaped field for this Model (i.e. id becomes `Post`.`id`).
|
|
|
|
*/
|
|
|
|
function escapeField($field)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
return $this->db->name($this->name).'.'.$this->db->name($field);
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the current record's ID
|
|
|
|
*
|
2006-01-17 17:52:23 +00:00
|
|
|
* @param unknown_type $list
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return mixed The ID of the current record
|
|
|
|
*/
|
|
|
|
function getID($list = 0)
|
|
|
|
{
|
|
|
|
if (!is_array($this->id))
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
if (count($this->id) == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isset($this->id[$list]))
|
|
|
|
{
|
|
|
|
return $this->id[$list];
|
|
|
|
}
|
|
|
|
foreach ($this->id as $id)
|
|
|
|
{
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns the ID of the last record this Model inserted
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function getLastInsertID ()
|
|
|
|
{
|
|
|
|
return $this->getInsertID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns the ID of the last record this Model inserted
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function getInsertID ()
|
|
|
|
{
|
|
|
|
return $this->__insertID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns the number of rows returned from the last query
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function getNumRows ()
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
//return $this->__numRows;
|
2006-01-15 12:18:57 +00:00
|
|
|
return $this->db->lastNumRows();
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* Returns the number of rows affected by the last query
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function getAffectedRows ()
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
//return $this->__affectedRows;
|
2006-01-15 12:18:57 +00:00
|
|
|
return $this->db->lastAffected();
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the DataSource to which this model is bound
|
|
|
|
*
|
|
|
|
* @param string $dataSource The name of the DataSource, as defined in Connections.php
|
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
|
|
|
function setDataSource($dataSource = null)
|
|
|
|
{
|
|
|
|
if ($dataSource == null)
|
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
$dataSource = $this->useDbConfig;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-01-12 03:56:59 +00:00
|
|
|
$this->db =& ConnectionManager::getDataSource($dataSource);
|
2006-02-03 04:45:08 +00:00
|
|
|
|
|
|
|
if(!empty($this->db->config['prefix']))
|
|
|
|
{
|
|
|
|
$this->tablePrefix = $this->db->config['prefix'];
|
|
|
|
}
|
2006-01-12 03:56:59 +00:00
|
|
|
if(empty($this->db) || $this->db == null || !is_object($this->db))
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
2006-01-12 03:56:59 +00:00
|
|
|
return $this->cakeError('missingConnection',array(array('className' => $this->name)));
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Before find callback
|
|
|
|
*
|
2006-03-12 00:11:40 +00:00
|
|
|
* @param array $queryData Data used to execute this query, i.e. conditions, order, etc.
|
2006-01-12 02:10:47 +00:00
|
|
|
* @return boolean True if the operation should continue, false if it should abort
|
|
|
|
*/
|
2006-03-12 00:11:40 +00:00
|
|
|
function beforeFind(&$queryData)
|
2006-01-12 02:10:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-17 10:12:15 +00:00
|
|
|
* After find callback. Can be used to modify any results returned by find and findAll.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @param mixed $results The results of the find operation
|
|
|
|
* @return mixed Result of the find operation
|
|
|
|
*/
|
|
|
|
function afterFind($results)
|
|
|
|
{
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Before save callback
|
|
|
|
*
|
|
|
|
* @return boolean True if the operation should continue, false if it should abort
|
|
|
|
*/
|
|
|
|
function beforeSave()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After save callback
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function afterSave()
|
|
|
|
{
|
2006-01-20 07:46:14 +00:00
|
|
|
return true;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Before delete callback
|
|
|
|
*
|
|
|
|
* @return boolean True if the operation should continue, false if it should abort
|
|
|
|
*/
|
|
|
|
function beforeDelete()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-03-12 00:11:40 +00:00
|
|
|
* After delete callback
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function afterDelete()
|
|
|
|
{
|
2006-01-20 07:46:14 +00:00
|
|
|
return true;
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-03-12 00:11:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Before validate callback
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function beforeValidate()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2006-03-12 06:58:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param string $type If null this deletes cached views if CACHE_CHECK is true
|
|
|
|
* Will be used to allow deleting query cache also
|
|
|
|
* @return boolean true on delete
|
|
|
|
*/
|
|
|
|
function _clearCache($type = null)
|
|
|
|
{
|
|
|
|
if($type === null)
|
|
|
|
{
|
|
|
|
if(defined('CACHE_CHECK') && CACHE_CHECK === true)
|
|
|
|
{
|
2006-03-12 07:23:40 +00:00
|
|
|
$assoc[] = low(Inflector::pluralize($this->name));
|
2006-03-12 06:58:53 +00:00
|
|
|
foreach ($this->__associations as $key => $asscociation)
|
|
|
|
{
|
|
|
|
foreach ($this->$asscociation as $key => $className)
|
|
|
|
{
|
|
|
|
$check = low(Inflector::pluralize($className['className']));
|
|
|
|
if(!in_array($check, $assoc))
|
|
|
|
{
|
|
|
|
$assoc[] = low(Inflector::pluralize($className['className']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-12 07:23:40 +00:00
|
|
|
clearCache($assoc);
|
|
|
|
return true;
|
2006-03-12 06:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Will use for query cache deleting
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- PHP4 Only
|
|
|
|
overload('Model');
|
|
|
|
// --- PHP4 Only
|
|
|
|
|
|
|
|
?>
|