📄 model_php5.php.svn-base
字号:
<?php/* SVN FILE: $Id: model_php5.php 4202 2006-12-25 12:06:13Z phpnut $ *//** * Object-relational mapper. * * DBO-backed object data model, for mapping database tables to Cake objects. * * PHP versions 5 * * CakePHP : Rapid Development Framework <http://www.cakephp.org/> * Copyright (c) 2006, Cake Software Foundation, Inc. * 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 * @copyright Copyright (c) 2006, Cake Software Foundation, Inc. * @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: 4202 $ * @modifiedby $LastChangedBy: phpnut $ * @lastmodified $Date: 2006-12-25 06:06:13 -0600 (Mon, 25 Dec 2006) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License *//** * Included libs */uses('class_registry', 'validators', 'model' . DS . 'connection_manager');/** * 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 Object{/** * 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 * * @var string * @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 * @access protected */ var $_tableInfo = null;/** * 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 * @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 * @access public */ var $name = null;/** * Name of the current model. * * @var string * @access public */ var $currentModel = null;/** * List of table names included in the Model description. Used for associations. * * @var array * @access public */ var $tableToModel = array();/** * List of Model names by used tables. Used for associations. * * @var array * @access public */ var $modelToTable = array();/** * List of Foreign Key names to used tables. Used for associations. * * @var array * @access public */ var $keyToTable = array();/** * Alias table names for model, for use in SQL JOIN statements. * * @var array * @access public */ var $alias = 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 */ var $cacheQueries = true;/** * 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();/** * Depth of recursive association * * @var int * @access public */ var $recursive = 1;/** * Default association keys * * @var array * @access private */ var $__associationKeys = array('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', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery'));/** * Holds provided/generated association key names and other data for all associations * * @var array * @access private */ var $__associations = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');/** * The last inserted ID of the data that this model created * * @var int * @access private */ var $__insertID = null;/** * The number of records returned by the last query * * @var int * @access private */ var $__numRows = null;/** * The number of records affected by the last query * * @var int * @access private */ var $__affectedRows = null;/** * Constructor. Binds the Model's database table to the object. * * @param integer $id * @param string $table Name of database table to use. * @param DataSource $ds DataSource connection object. */ 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) { $this->setDataSource($ds); if ($table) { $tableName = $table; } else { if ($this->useTable) { $tableName = $this->useTable; } else { $tableName = Inflector::tableize($this->name); } } if (in_array('settableprefix', get_class_methods($this))) { $this->setTablePrefix(); } $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; } } } }/** * Handles custom method calls, like findBy<field> for DB models, * and custom RPC calls for remote data sources * * @param unknown_type $method * @param array $params * @return unknown * @access protected */ function __call($method, $params) { $db =& ConnectionManager::getDataSource($this->useDbConfig); return $db->query($method, $params, $this); }/** * Bind model associations on the fly. * * @param array $params * @return true * @access public */ function bindModel($params) { foreach($params as $assoc => $model) { $this->__backAssociation[$assoc] = $this->{$assoc}; foreach($model as $key => $value) { $assocName = $key; $modelName = $key; if (isset($value['className'])) { $modelName = $value['className']; } $this->__constructLinkedModel($assocName, $modelName); $this->{$assoc}[$assocName] = $model[$assocName]; $this->__generateAssociation($assoc); } } return true; }/** * Turn off associations on the fly. * * @param array $params * @return true * @access public */ function unbindModel($params) { foreach($params as $assoc => $models) { $this->__backAssociation[$assoc] = $this->{$assoc}; foreach($models as $model) { $this->__backAssociation = array_merge($this->__backAssociation, $this->{$assoc}); unset ($this->{$assoc}[$model]); } } return true; }/** * Private helper method to create a set of associations. * * @access private */ function __createLinks() { // Convert all string-based associations to array based 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) { if (is_numeric($assoc)) { unset ($this->{$type}[$assoc]); $assoc = $value; $value = array(); $this->{$type}[$assoc] = $value; } $className = $assoc; if (isset($value['className']) && !empty($value['className'])) { $className = $value['className']; } $this->__constructLinkedModel($assoc, $className); } } foreach($this->__associations as $type) { $this->__generateAssociation($type); } }/** * 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 */ function __constructLinkedModel($assoc, $className) { $colKey = Inflector::underscore($className); if(!class_exists($className)){ loadModel($className); } if (ClassRegistry::isKeySet($colKey)) { $this->{$assoc} = ClassRegistry::getObject($colKey); $this->{$className} = $this->{$assoc}; } else { $this->{$assoc} = new $className(); $this->{$className} = $this->{$assoc}; } $this->alias[$assoc] = $this->{$assoc}->table; $this->tableToModel[$this->{$assoc}->table] = $className; $this->modelToTable[$assoc] = $this->{$assoc}->table; }/** * Build array-based association from string. * * @param string $type "Belongs", "One", "Many", "ManyTo" * @access private */ function __generateAssociation($type) { foreach($this->{$type}as $assocKey => $assocData) { $class = $assocKey; foreach($this->__associationKeys[$type] as $key) { if (!isset($this->{$type}[$assocKey][$key]) || $this->{$type}[$assocKey][$key] == null) { $data = ''; switch($key) { case 'fields': $data = ''; break; case 'foreignKey': $data = Inflector::singularize($this->table) . '_id'; if ($type == 'belongsTo') { $data = Inflector::singularize($this->{$class}->table) . '_id'; } break; case 'associationForeignKey': $data = Inflector::singularize($this->{$class}->table) . '_id'; break; case 'joinTable': $tables = array($this->table, $this->{$class}->table); sort ($tables); $data = $tables[0] . '_' . $tables[1]; break; case 'className': $data = $class; break; } $this->{$type}[$assocKey][$key] = $data; } 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; } } } }/** * 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 * @access public */ function setSource($tableName) { $db =& ConnectionManager::getDataSource($this->useDbConfig); if ($db->isInterfaceSupported('listSources')) { $prefix = ''; if ($this->tablePrefix) { $prefix = $this->tablePrefix; } $sources = $db->listSources(); if (is_array($sources) && !in_array(low($prefix . $tableName), array_map('low', $sources))) { return $this->cakeError('missingTable', array(array( 'className' => $this->name, 'table' => $prefix . $tableName))); } else { $this->table = $tableName; $this->tableToModel[$this->table] = $this->name; $this->loadInfo(); } } else { $this->table = $tableName; $this->tableToModel[$this->table] = $this->name; $this->loadInfo(); } }/** * 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 array * @access public */ function set($one, $two = null) { if (is_array($one)) { if (countdim($one) == 1) { $data = array($this->name => $one); } else { $data = $one; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -