📄 model_php5.php.svn-base
字号:
} return $data[0]; }/** * Returns a resultset array with specified fields from database matching given conditions. * By using the $recursive parameter, the call can access further "levels of association" than * the ones this model is directly associated to. * * @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 int $page Page number, for accessing paged data * @param int $recursive The number of levels deep to fetch associated records * @return array Array of records * @access public */ function findAll($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) { $db =& ConnectionManager::getDataSource($this->useDbConfig); $this->id = $this->getID(); $offset = null; if ($page > 1 && $limit != null) { $offset = ($page - 1) * $limit; } if ($order == null) { $order = array(); } else { $order = array($order); } $queryData = array('conditions' => $conditions, 'fields' => $fields, 'joins' => array(), 'limit' => $limit, 'offset' => $offset, 'order' => $order ); $ret = $this->beforeFind($queryData); if (is_array($ret)) { $queryData = $ret; } elseif ($ret === false) { return null; } $return = $this->afterFind($db->read($this, $queryData, $recursive)); if (isset($this->__backAssociation)) { $this->__resetAssociations(); } 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 boolean * @access private */ function __resetAssociations() { foreach($this->__associations as $type) { if (isset($this->__backAssociation[$type])) { $this->{$type} = $this->__backAssociation[$type]; } } unset ($this->__backAssociation); return true; }/** * Runs a direct query against the bound DataSource, and returns the result. * * @param string $data Query data * @return array * @access public */ function execute($data) { $db =& ConnectionManager::getDataSource($this->useDbConfig); $data = $db->fetchAll($data, $this->cacheQueries); 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; }/** * Returns number of rows matching given SQL condition. * * @param array $conditions SQL conditions array for findAll * @param int $recursize The number of levels deep to fetch associated records * @return int Number of matching rows * @see Model::findAll * @access public */ function findCount($conditions = null, $recursive = 0) { list($data) = $this->findAll($conditions, 'COUNT(*) AS count', null, null, 1, $recursive); if (isset($data[0]['count'])) { return $data[0]['count']; } elseif (isset($data[$this->name]['count'])) { return $data[$this->name]['count']; } return false; }/** * 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 * @return array * @access public * @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. * * @param array $data * @param string $root NULL or id for root node of operation * @return array * @access private * @see findAllThreaded */ function __doThread($data, $root) { $out = array(); $sizeOf = sizeof($data); for($ii = 0; $ii < $sizeOf; $ii++) { if (($data[$ii][$this->name]['parent_id'] == $root) || (($root === null) && ($data[$ii][$this->name]['parent_id'] == '0'))) { $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; }/** * 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 * @param string $field Field name (parameter for findAll) * @param unknown_type $value * @return array Array with keys "prev" and "next" that holds the id's * @access public */ function findNeighbours($conditions = null, $field, $value) { $db =& ConnectionManager::getDataSource($this->useDbConfig); if (!is_null($conditions)) { $conditions = $conditions . ' AND '; } @list($prev) = Model::findAll($conditions . $field . ' < ' . $db->value($value), $field, $field . ' DESC', 1, null, 0); @list($next) = Model::findAll($conditions . $field . ' > ' . $db->value($value), $field, $field . ' ASC', 1, null, 0); if (!isset($prev)) { $prev = null; } if (!isset($next)) { $next = null; } return array('prev' => $prev, 'next' => $next); }/** * Returns a resultset for given SQL statement. Generic SQL queries should be made with this method. * * @param string $sql SQL statement * @return array Resultset * @access public */ function query() { $params = func_get_args(); $db =& ConnectionManager::getDataSource($this->useDbConfig); return call_user_func_array(array(&$db, 'query'), $params); }/** * Returns true if all fields pass validation, otherwise false. * * @param array $data POST data * @return boolean True if there are no errors * @access public */ function validates($data = array()) { $errors = $this->invalidFields($data); return count($errors) == 0; }/** * Returns an array of invalid fields. * * @param array $data * @return array Array of invalid fields * @access public */ function invalidFields($data = array()) { if (empty($data)) { $data = $this->data; } if (!$this->beforeValidate()) { return false; } if (!isset($this->validate)) { return true; } if (!empty($data)) { $data = $data; } elseif (isset($this->data)) { $data = $this->data; } if (isset($data[$this->name])) { $data = $data[$this->name]; } foreach($this->validate as $field_name => $validator) { if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name])) { $this->invalidate($field_name); } } return $this->validationErrors; }/** * Sets a field as invalid * * @param string $field The name of the field to invalidate * @return void * @access public */ function invalidate($field) { if (!is_array($this->validationErrors)) { $this->validationErrors = array(); } $this->validationErrors[$field] = 1; }/** * 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" * @return True if the field is a foreign key listed in the belongsTo array. * @access public */ function isForeignKey($field) { $foreignKeys = array(); if (count($this->belongsTo)) { foreach($this->belongsTo as $assoc => $data) { $foreignKeys[] = $data['foreignKey']; } } return (bool)(in_array($field, $foreignKeys)); }/** * Gets the display field for this model * * @return string The name of the display field for this Model (i.e. 'name', 'title'). * @access public */ function getDisplayField() { return $this->displayField; }/** * Returns a resultset array with specified fields from database matching given conditions. * Method can be used to generate option lists for SELECT elements. * * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) * @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 * @access public */ function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null) { $db =& ConnectionManager::getDataSource($this->useDbConfig); if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) { $fields = array($this->primaryKey, $this->displayField); } else { $fields = null; } $recursive = $this->recursive; if($recursive >= 1) { $this->recursive = -1; } $result = $this->findAll($conditions, $fields, $order, $limit); $this->recursive = $recursive; if ($keyPath == null) { $keyPath = '{n}.' . $this->name . '.' . $this->primaryKey; } if ($valuePath == null) { $valuePath = '{n}.' . $this->name . '.' . $this->displayField; } $keys = $db->getFieldValue($result, $keyPath); $vals = $db->getFieldValue($result, $valuePath); if (!empty($keys) && !empty($vals)) { $return = array_combine($keys, $vals); return $return; } }/** * Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules. * * @param unknown_type $field * @return string The name of the escaped field for this Model (i.e. id becomes `Post`.`id`). * @access public */ function escapeField($field) { $db =& ConnectionManager::getDataSource($this->useDbConfig); return $db->name($this->name) . '.' . $db->name($field); }/** * Returns the current record's ID * * @param unknown_type $list * @return mixed The ID of the current record * @access public */ 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; }/** * Returns the ID of the last record this Model inserted * * @return mixed * @access public */ function getLastInsertID() { return $this->getInsertID(); }/** * Returns the ID of the last record this Model inserted * * @return mixed * @access public */ function getInsertID() { return $this->__insertID; }/** * Returns the number of rows returned from the last query * * @return int * @access public */ function getNumRows() { $db =& ConnectionManager::getDataSource($this->useDbConfig); return $db->lastNumRows(); }/** * Returns the number of rows affected by the last query * * @return int * @access public */ function getAffectedRows() { $db =& ConnectionManager::getDataSource($this->useDbConfig); 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 * @return boolean True on success * @access public */ function setDataSource($dataSource = null) { if ($dataSource == null) { $dataSource = $this->useDbConfig; } $db =& ConnectionManager::getDataSource($dataSource); if (!empty($db->config['prefix']) && $this->tablePrefix == null) { $this->tablePrefix = $db->config['prefix']; } if (empty($db) || $db == null || !is_object($db)) { return $this->cakeError('missingConnection', array(array('className' => $this->name))); } }/** * Before find callback * * @param array $queryData Data used to execute this query, i.e. conditions, order, etc. * @return boolean True if the operation should continue, false if it should abort * @access public */ function beforeFind(&$queryData) { return true; }/** * After find callback. Can be used to modify any results returned by find and findAll. * * @param mixed $results The results of the find operation * @return mixed Result of the find operation * @access public */ function afterFind($results) { return $results; }/** * Before save callback * * @return boolean True if the operation should continue, false if it should abort * @access public */ function beforeSave() { return true; }/** * After save callback * * @return boolean * @access public */ function afterSave() { return true; }/** * Before delete callback * * @return boolean True if the operation should continue, false if it should abort * @access public */ function beforeDelete() { return true; }/** * After delete callback * * @return boolean * @access public */ function afterDelete() { return true; }/** * Before validate callback * * @return boolean * @access public */ function beforeValidate() { return true; }/** * Private method. Clears cache for this model * * @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 * @access protected */ function _clearCache($type = null) { if ($type === null) { if (defined('CACHE_CHECK') && CACHE_CHECK === true) { $assoc[] = strtolower(Inflector::pluralize($this->name)); foreach($this->__associations as $key => $association) { foreach($this->$association as $key => $className) { $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 * * @return array * @access public */ function __sleep() { $return = array_keys(get_object_vars($this)); return $return; }/** * Called when unserializing a model * * @return void * @access public */ function __wakeup() { }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -