From 09bdad6ceb2b2bf066fe1f3f785f4cbf8ab63cc2 Mon Sep 17 00:00:00 2001 From: Kim Egede Jakobsen Date: Sun, 1 Sep 2013 12:08:48 -0700 Subject: [PATCH 1/2] Reduce variable use/duplicate code. --- lib/Cake/Model/Model.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 0c1d51e02..662dcc3d1 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -796,8 +796,8 @@ class Model extends Object implements CakeEventListener { if ($result !== array('unhandled')) { return $result; } - $return = $this->getDataSource()->query($method, $params, $this); - return $return; + + return $this->getDataSource()->query($method, $params, $this); } /** @@ -2957,8 +2957,9 @@ class Model extends Object implements CakeEventListener { * @return array */ protected function _findNeighbors($state, $query, $results = array()) { + extract($query); + if ($state === 'before') { - extract($query); $conditions = (array)$conditions; if (isset($field) && isset($value)) { if (strpos($field, '.') === false) { @@ -2976,7 +2977,6 @@ class Model extends Object implements CakeEventListener { return $query; } - extract($query); unset($query['conditions'][$field . ' <']); $return = array(); if (isset($results[0])) { From 63dfbe55f708c85454cd5b5e81e127ab134df1fe Mon Sep 17 00:00:00 2001 From: Kim Egede Jakobsen Date: Sun, 1 Sep 2013 12:09:14 -0700 Subject: [PATCH 2/2] Added/removed newlines to make the code more readable. --- lib/Cake/Model/Model.php | 182 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 662dcc3d1..7af3e8d02 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -780,6 +780,7 @@ class Model extends Object implements CakeEventListener { $this->_eventManager->attach($this->Behaviors); $this->_eventManager->attach($this); } + return $this->_eventManager; } @@ -822,6 +823,7 @@ class Model extends Object implements CakeEventListener { if (empty($relation['with'])) { continue; } + if (is_array($relation['with'])) { if (key($relation['with']) === $name) { $className = $name; @@ -832,6 +834,7 @@ class Model extends Object implements CakeEventListener { $className = $relation['with']; } } + if ($className) { $assocKey = $k; $dynamic = !empty($relation['dynamicWith']); @@ -877,13 +880,16 @@ class Model extends Object implements CakeEventListener { if ($name === 'displayField') { return $this->displayField = $this->hasField(array('title', 'name', $this->primaryKey)); } + if ($name === 'tablePrefix') { $this->setDataSource(); if (property_exists($this, 'tablePrefix') && !empty($this->tablePrefix)) { return $this->tablePrefix; } + return $this->tablePrefix = null; } + if (isset($this->{$name})) { return $this->{$name}; } @@ -913,6 +919,7 @@ class Model extends Object implements CakeEventListener { if ($reset === true && !isset($this->__backAssociation[$assoc])) { $this->__backAssociation[$assoc] = $this->{$assoc}; } + foreach ($model as $key => $value) { $assocName = $key; @@ -920,15 +927,19 @@ class Model extends Object implements CakeEventListener { $assocName = $value; $value = array(); } + $this->{$assoc}[$assocName] = $value; + if (property_exists($this, $assocName)) { unset($this->{$assocName}); } + if ($reset === false && isset($this->__backAssociation[$assoc])) { $this->__backAssociation[$assoc][$assocName] = $value; } } } + $this->_createLinks(); return true; } @@ -956,13 +967,16 @@ class Model extends Object implements CakeEventListener { if ($reset === true && !isset($this->__backAssociation[$assoc])) { $this->__backAssociation[$assoc] = $this->{$assoc}; } + foreach ($models as $model) { if ($reset === false && isset($this->__backAssociation[$assoc][$model])) { unset($this->__backAssociation[$assoc][$model]); } + unset($this->{$assoc}[$model]); } } + return true; } @@ -999,6 +1013,7 @@ class Model extends Object implements CakeEventListener { $this->{$type}[$assoc] = $value; } } + $this->_generateAssociation($type, $assoc); } } @@ -1027,11 +1042,14 @@ class Model extends Object implements CakeEventListener { if ($plugin) { $plugin .= '.'; } + $model = array('class' => $plugin . $className, 'alias' => $assoc); $this->{$assoc} = ClassRegistry::init($model); + if ($plugin) { ClassRegistry::addObject($plugin . $className, $this->{$assoc}); } + if ($assoc) { $this->tableToModel[$this->{$assoc}->table] = $assoc; } @@ -1050,7 +1068,6 @@ class Model extends Object implements CakeEventListener { $dynamicWith = false; foreach ($this->_associationKeys[$type] as $key) { - if (!isset($this->{$type}[$assocKey][$key]) || $this->{$type}[$assocKey][$key] === null) { $data = ''; @@ -1086,13 +1103,13 @@ class Model extends Object implements CakeEventListener { $data = true; break; } + $this->{$type}[$assocKey][$key] = $data; } if ($dynamicWith) { $this->{$type}[$assocKey]['dynamicWith'] = true; } - } } @@ -1117,8 +1134,10 @@ class Model extends Object implements CakeEventListener { 'ds' => $this->useDbConfig, )); } + $this->_schema = null; } + $this->table = $this->useTable = $tableName; $this->tableToModel[$this->table] = $this->alias; } @@ -1142,6 +1161,7 @@ class Model extends Object implements CakeEventListener { if (!$one) { return; } + if (is_object($one)) { if ($one instanceof SimpleXMLElement || $one instanceof DOMNode) { $one = $this->_normalizeXmlData(Xml::toArray($one)); @@ -1161,7 +1181,6 @@ class Model extends Object implements CakeEventListener { foreach ($data as $modelName => $fieldSet) { if (is_array($fieldSet)) { - foreach ($fieldSet as $fieldName => $fieldValue) { if (isset($this->validationErrors[$fieldName])) { unset($this->validationErrors[$fieldName]); @@ -1172,13 +1191,16 @@ class Model extends Object implements CakeEventListener { $this->id = $fieldValue; } } + if (is_array($fieldValue) || is_object($fieldValue)) { $fieldValue = $this->deconstruct($fieldName, $fieldValue); } + $this->data[$modelName][$fieldName] = $fieldValue; } } } + return $data; } @@ -1191,12 +1213,14 @@ class Model extends Object implements CakeEventListener { protected function _setAliasData($data) { $models = array_keys($this->getAssociated()); $schema = array_keys((array)$this->schema()); + foreach ($data as $field => $value) { if (in_array($field, $schema) || !in_array($field, $models)) { $data[$this->alias][$field] = $value; unset($data[$field]); } } + return $data; } @@ -1217,6 +1241,7 @@ class Model extends Object implements CakeEventListener { $return[$key] = $value; } } + return $return; } @@ -1258,9 +1283,11 @@ class Model extends Object implements CakeEventListener { ) { $data['hour'] = $data['hour'] + 12; } + if (isset($data['hour']) && isset($data['meridian']) && $data['hour'] == 12 && $data['meridian'] === 'am') { $data['hour'] = '00'; } + if ($type === 'time') { foreach ($timeFields as $key => $val) { if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') { @@ -1268,6 +1295,7 @@ class Model extends Object implements CakeEventListener { } elseif ($data[$val] !== '') { $data[$val] = sprintf('%02d', $data[$val]); } + if (!empty($data[$val])) { $date[$key] = $data[$val]; } else { @@ -1285,9 +1313,11 @@ class Model extends Object implements CakeEventListener { $data[$val] = sprintf('%02d', $data[$val]); } } + if (!isset($data[$val]) || isset($data[$val]) && (empty($data[$val]) || $data[$val][0] === '-')) { return null; } + if (isset($data[$val]) && !empty($data[$val])) { $date[$key] = $data[$val]; } @@ -1301,8 +1331,10 @@ class Model extends Object implements CakeEventListener { $date[$index] = sprintf('%02d', $date[$index]); } } + return str_replace(array_keys($date), array_values($date), $format); } + return $data; } @@ -1321,12 +1353,15 @@ class Model extends Object implements CakeEventListener { $this->_schema = $db->describe($this); } } + if (!is_string($field)) { return $this->_schema; } + if (isset($this->_schema[$field])) { return $this->_schema[$field]; } + return null; } @@ -1340,10 +1375,12 @@ class Model extends Object implements CakeEventListener { if (empty($columns)) { trigger_error(__d('cake_dev', '(Model::getColumnTypes) Unable to build model field data. If you are using a model without a database table, try implementing schema()'), E_USER_WARNING); } + $cols = array(); foreach ($columns as $field => $values) { $cols[$field] = $values['type']; } + return $cols; } @@ -1365,12 +1402,15 @@ class Model extends Object implements CakeEventListener { if (strpos($column, '.')) { list($model, $column) = explode('.', $column); } + if ($model != $this->alias && isset($this->{$model})) { return $this->{$model}->getColumnType($column); } + if (isset($cols[$column]) && isset($cols[$column]['type'])) { return $cols[$column]['type']; } + return null; } @@ -1390,6 +1430,7 @@ class Model extends Object implements CakeEventListener { return $n; } } + return false; } @@ -1404,6 +1445,7 @@ class Model extends Object implements CakeEventListener { if ($this->_schema) { return isset($this->_schema[$name]); } + return false; } @@ -1418,6 +1460,7 @@ class Model extends Object implements CakeEventListener { if (method_exists($this, $method)) { return true; } + return $this->Behaviors->hasMethod($method); } @@ -1431,15 +1474,18 @@ class Model extends Object implements CakeEventListener { if (empty($this->virtualFields) || !is_string($field)) { return false; } + if (isset($this->virtualFields[$field])) { return true; } + if (strpos($field, '.') !== false) { list($model, $field) = explode('.', $field); if ($model == $this->alias && isset($this->virtualFields[$field])) { return true; } } + return false; } @@ -1455,12 +1501,15 @@ class Model extends Object implements CakeEventListener { if (!$field) { return empty($this->virtualFields) ? false : $this->virtualFields; } + if ($this->isVirtualField($field)) { if (strpos($field, '.') !== false) { list(, $field) = pluginSplit($field); } + return $this->virtualFields[$field]; } + return false; } @@ -1488,12 +1537,15 @@ class Model extends Object implements CakeEventListener { $defaults[$field] = $properties['default']; } } + $this->set($defaults); $this->set($data); } + if ($filterKey) { $this->set($this->primaryKey, false); } + return $this->data; } @@ -1535,8 +1587,10 @@ class Model extends Object implements CakeEventListener { 'conditions' => array($this->alias . '.' . $this->primaryKey => $id), 'fields' => $fields )); + return $this->data; } + return false; } @@ -1554,15 +1608,18 @@ class Model extends Object implements CakeEventListener { if ($conditions === null && $this->id !== false) { $conditions = array($this->alias . '.' . $this->primaryKey => $this->id); } + $recursive = $this->recursive; if ($this->recursive >= 1) { $recursive = -1; } + $fields = $name; $data = $this->find('first', compact('conditions', 'fields', 'order', 'recursive')); if (!$data) { return false; } + if (strpos($name, '.') === false) { if (isset($data[$this->alias][$name])) { return $data[$this->alias][$name]; @@ -1573,6 +1630,7 @@ class Model extends Object implements CakeEventListener { return $data[$name[0]][$name[1]]; } } + if (isset($data[0]) && count($data[0]) > 0) { return array_shift($data[0]); } @@ -1600,6 +1658,7 @@ class Model extends Object implements CakeEventListener { if (is_array($validate)) { $options = array_merge(array('validate' => false, 'fieldList' => array($name)), $validate); } + return $this->save(array($this->alias => array($this->primaryKey => $id, $name => $value)), $options); } @@ -1645,6 +1704,7 @@ class Model extends Object implements CakeEventListener { } elseif ($options['fieldList'] === null) { $this->whitelist = array(); } + $this->set($data); if (empty($this->data) && !$this->hasField(array('created', 'updated', 'modified'))) { @@ -1658,6 +1718,7 @@ class Model extends Object implements CakeEventListener { array_key_exists($field, $this->data[$this->alias]) && $this->data[$this->alias][$field] === null ); + if ($keyPresentAndEmpty) { unset($this->data[$this->alias][$field]); } @@ -1669,9 +1730,11 @@ class Model extends Object implements CakeEventListener { if (!$exists) { $dateFields[] = 'created'; } + if (isset($this->data[$this->alias])) { $fields = array_keys($this->data[$this->alias]); } + if ($options['validate'] && !$this->validates($options)) { $this->whitelist = $_whitelist; return false; @@ -1689,6 +1752,7 @@ class Model extends Object implements CakeEventListener { } else { $time = call_user_func($colType['formatter'], $colType['format']); } + if (!empty($this->whitelist)) { $this->whitelist[] = $updateCol; } @@ -1735,11 +1799,13 @@ class Model extends Object implements CakeEventListener { } } } + $count = count($fields); if (!$exists && $count > 0) { $this->id = false; } + $success = true; $created = false; @@ -1784,17 +1850,21 @@ class Model extends Object implements CakeEventListener { $this->data[$this->alias][$this->primaryKey] = $this->id; } } + if ($options['callbacks'] === true || $options['callbacks'] === 'after') { $event = new CakeEvent('Model.afterSave', $this, array($created, $options)); $this->getEventManager()->dispatch($event); } + if (!empty($this->data)) { $success = $this->data; } + $this->data = false; $this->_clearCache(); $this->validationErrors = array(); } + $this->whitelist = $_whitelist; return $success; } @@ -1820,7 +1890,6 @@ class Model extends Object implements CakeEventListener { */ protected function _saveMulti($joined, $id, $db) { foreach ($joined as $assoc => $data) { - if (isset($this->hasAndBelongsToMany[$assoc])) { list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); @@ -1852,20 +1921,24 @@ class Model extends Object implements CakeEventListener { if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) { $newJoins[] = $row; $values = array($id, $row); + if ($isUUID && $primaryAdded) { $values[] = String::uuid(); } + $newValues[$row] = $values; unset($values); } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { if (!empty($row[$this->{$join}->primaryKey])) { $newJoins[] = $row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']]; } + $newData[] = $row; } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { if (!empty($row[$join][$this->{$join}->primaryKey])) { $newJoins[] = $row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']]; } + $newData[] = $row[$join]; } } @@ -1875,9 +1948,11 @@ class Model extends Object implements CakeEventListener { $conditions = array( $join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id ); + if (!empty($this->hasAndBelongsToMany[$assoc]['conditions'])) { $conditions = array_merge($conditions, (array)$this->hasAndBelongsToMany[$assoc]['conditions']); } + $associationForeignKey = $this->{$join}->alias . '.' . $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; $links = $this->{$join}->find('all', array( 'conditions' => $conditions, @@ -1892,6 +1967,7 @@ class Model extends Object implements CakeEventListener { } else { $conditions[$associationForeignKey] = $oldLinks; } + $dbMulti->delete($this->{$join}, $conditions); } } @@ -1902,6 +1978,7 @@ class Model extends Object implements CakeEventListener { if (empty($data[$this->{$join}->primaryKey])) { $this->{$join}->create(); } + $this->{$join}->save($data); } } @@ -1917,8 +1994,10 @@ class Model extends Object implements CakeEventListener { unset($newValues[$oldJoin]); } } + $newValues = array_values($newValues); } + if (!empty($newValues)) { $dbMulti->insertMulti($this->{$join}, $fields, $newValues); } @@ -1956,9 +2035,11 @@ class Model extends Object implements CakeEventListener { if (!is_string($field)) { $field = Inflector::underscore($this->alias) . '_count'; } + if (!$this->{$parent}->hasField($field)) { continue; } + if ($conditions === true) { $conditions = array(); } else { @@ -1968,6 +2049,7 @@ class Model extends Object implements CakeEventListener { if (!array_key_exists($foreignKey, $keys)) { $keys[$foreignKey] = $this->field($foreignKey); } + $recursive = (empty($conditions) ? -1 : 0); if (isset($keys['old'][$foreignKey])) { @@ -1981,11 +2063,13 @@ class Model extends Object implements CakeEventListener { ); } } + $conditions[$fkQuoted] = $keys[$foreignKey]; if ($recursive === 0) { $conditions = array_merge($conditions, (array)$conditions); } + $count = intval($this->find('count', compact('conditions', 'recursive'))); $this->{$parent}->updateAll( @@ -2011,16 +2095,19 @@ class Model extends Object implements CakeEventListener { $foreignKeys[$assoc] = $info['foreignKey']; } } + $included = array_intersect($foreignKeys, array_keys($data)); if (empty($included) || empty($this->id)) { return array(); } + $old = $this->find('first', array( 'conditions' => array($this->alias . '.' . $this->primaryKey => $this->id), 'fields' => array_values($included), 'recursive' => -1 )); + return array_merge($data, array('old' => $old[$this->alias])); } @@ -2065,11 +2152,14 @@ class Model extends Object implements CakeEventListener { if ($options['validate'] === 'only') { return $this->validateMany($data, $options); } + return $this->saveMany($data, $options); } + if ($options['validate'] === 'only') { return $this->validateAssociated($data, $options); } + return $this->saveAssociated($data, $options); } @@ -2107,6 +2197,7 @@ class Model extends Object implements CakeEventListener { if (!$options['atomic']) { return array(!empty($result)); } + return !empty($result); } @@ -2122,6 +2213,7 @@ class Model extends Object implements CakeEventListener { $db = $this->getDataSource(); $transactionBegun = $db->begin(); } + $return = array(); foreach ($data as $key => $record) { $validates = $this->create(null) !== null; @@ -2133,27 +2225,32 @@ class Model extends Object implements CakeEventListener { $saved = $this->save($record, $options); } } + $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true)))); if (!$validates) { $validationErrors[$key] = $this->validationErrors; } + if (!$options['atomic']) { $return[$key] = $validates; } elseif (!$validates) { break; } } + $this->validationErrors = $validationErrors; if (!$options['atomic']) { return $return; } + if ($validates) { if ($transactionBegun) { return $db->commit() !== false; } return true; } + $db->rollback(); return false; } @@ -2222,6 +2319,7 @@ class Model extends Object implements CakeEventListener { if (!$options['atomic']) { return array(!empty($result)); } + return !empty($result); } @@ -2230,8 +2328,10 @@ class Model extends Object implements CakeEventListener { if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, Hash::flatten($validates), true))) { return $validates; } + $options['validate'] = false; } + if ($options['atomic']) { $db = $this->getDataSource(); $transactionBegun = $db->begin(); @@ -2253,6 +2353,7 @@ class Model extends Object implements CakeEventListener { } $validates = ($saved === true || (is_array($saved) && !in_array(false, $saved, true))); } + if ($validates) { $key = $this->belongsTo[$association]['foreignKey']; if (isset($data[$this->alias])) { @@ -2264,9 +2365,11 @@ class Model extends Object implements CakeEventListener { } else { $validationErrors[$association] = $this->{$association}->validationErrors; } + $return[$association] = $validates; } } + if ($validates && !($this->create(null) !== null && $this->save($data, $options))) { $validationErrors[$this->alias] = $this->validationErrors; $validates = false; @@ -2277,6 +2380,7 @@ class Model extends Object implements CakeEventListener { if (!$validates) { break; } + $notEmpty = !empty($values[$association]) || (!isset($values[$association]) && !empty($values)); if (isset($associations[$association]) && $notEmpty) { $type = $associations[$association]; @@ -2288,8 +2392,10 @@ class Model extends Object implements CakeEventListener { } else { $values = array_merge(array($key => $this->id), $values, array($key => $this->id)); } + $validates = $this->{$association}->create(null) !== null; $saved = false; + if ($validates) { $options = $this->{$association}->_addToWhiteList($key, $options); if ($options['deep']) { @@ -2298,10 +2404,12 @@ class Model extends Object implements CakeEventListener { $saved = $this->{$association}->save($values, $options); } } + $validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true)))); if (!$validates) { $validationErrors[$association] = $this->{$association}->validationErrors; } + $return[$association] = $validates; break; case 'hasMany': @@ -2312,12 +2420,14 @@ class Model extends Object implements CakeEventListener { $values[$i] = array_merge(array($key => $this->id), $value, array($key => $this->id)); } } + $options = $this->{$association}->_addToWhiteList($key, $options); $_return = $this->{$association}->saveMany($values, array_merge($options, array('atomic' => false))); if (in_array(false, $_return, true)) { $validationErrors[$association] = $this->{$association}->validationErrors; $validates = false; } + $return[$association] = $_return; break; } @@ -2338,8 +2448,10 @@ class Model extends Object implements CakeEventListener { if ($transactionBegun) { return $db->commit() !== false; } + return true; } + $db->rollback(); return false; } @@ -2357,16 +2469,19 @@ class Model extends Object implements CakeEventListener { $options['fieldList'][$this->alias][] = $key; return $options; } + if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) { $options['fieldList'][$this->alias][] = $key; return $options; } + if (!empty($options['fieldList']) && is_array($options['fieldList']) && Hash::dimensions($options['fieldList']) < 2 ) { $options['fieldList'][] = $key; } + return $options; } @@ -2418,6 +2533,7 @@ class Model extends Object implements CakeEventListener { if (!empty($id)) { $this->id = $id; } + $id = $this->id; $event = new CakeEvent('Model.beforeDelete', $this, array($cascade)); @@ -2426,6 +2542,7 @@ class Model extends Object implements CakeEventListener { if ($event->isStopped()) { return false; } + if (!$this->exists()) { return false; } @@ -2457,9 +2574,11 @@ class Model extends Object implements CakeEventListener { if (!empty($keys[$this->alias])) { $this->updateCounterCache($keys[$this->alias]); } + $this->getEventManager()->dispatch(new CakeEvent('Model.afterDelete', $this)); $this->_clearCache(); $this->id = false; + return true; } @@ -2474,6 +2593,7 @@ class Model extends Object implements CakeEventListener { if ($cascade !== true) { return; } + if (!empty($this->__backAssociation)) { $savedAssociations = $this->__backAssociation; $this->__backAssociation = array(); @@ -2511,6 +2631,7 @@ class Model extends Object implements CakeEventListener { } } } + if (isset($savedAssociations)) { $this->__backAssociation = $savedAssociations; } @@ -2531,6 +2652,7 @@ class Model extends Object implements CakeEventListener { 'recursive' => -1, 'callbacks' => false )); + if (!empty($records)) { foreach ($records as $record) { $this->{$joinModel}->delete($record[$this->{$joinModel}->alias][$this->{$joinModel}->primaryKey]); @@ -2552,15 +2674,18 @@ class Model extends Object implements CakeEventListener { if (empty($conditions)) { return false; } + $db = $this->getDataSource(); if (!$cascade && !$callbacks) { return $db->delete($this, $conditions); } + $ids = $this->find('all', array_merge(array( 'fields' => "{$this->alias}.{$this->primaryKey}", 'recursive' => 0), compact('conditions')) ); + if ($ids === false || $ids === null) { return false; } @@ -2576,6 +2701,7 @@ class Model extends Object implements CakeEventListener { foreach ($ids as $id) { $result = $result && $this->delete($id, $cascade); } + $this->id = $_id; return $result; } @@ -2586,6 +2712,7 @@ class Model extends Object implements CakeEventListener { $this->_deleteDependent($id, $cascade); } } + return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids)); } @@ -2603,6 +2730,7 @@ class Model extends Object implements CakeEventListener { $result[$assoc] = $data['foreignKey']; } } + return $result; } @@ -2620,9 +2748,11 @@ class Model extends Object implements CakeEventListener { if ($id === null) { $id = $this->getID(); } + if ($id === false) { return false; } + return (bool)$this->find('count', array( 'conditions' => array( $this->alias . '.' . $this->primaryKey => $id @@ -2785,21 +2915,26 @@ class Model extends Object implements CakeEventListener { if (!is_numeric($query['page']) || intval($query['page']) < 1) { $query['page'] = 1; } + if ($query['page'] > 1 && !empty($query['limit'])) { $query['offset'] = ($query['page'] - 1) * $query['limit']; } + if ($query['order'] === null && $this->order !== null) { $query['order'] = $this->order; } + $query['order'] = array($query['order']); if ($query['callbacks'] === true || $query['callbacks'] === 'before') { $event = new CakeEvent('Model.beforeFind', $this, array($query)); list($event->break, $event->breakOn, $event->modParams) = array(true, array(false, null), 0); $this->getEventManager()->dispatch($event); + if ($event->isStopped()) { return null; } + $query = $event->result === true ? $event->data[0] : $event->result; } @@ -2841,6 +2976,7 @@ class Model extends Object implements CakeEventListener { if (empty($results[0])) { return array(); } + return $results[0]; } @@ -2859,16 +2995,19 @@ class Model extends Object implements CakeEventListener { $query['operation'] = 'count'; $query = $this->{'_find' . ucfirst($query['type'])}('before', $query); } + $db = $this->getDataSource(); $query['order'] = false; if (!method_exists($db, 'calculate')) { return $query; } + if (!empty($query['fields']) && is_array($query['fields'])) { if (!preg_match('/^count/i', current($query['fields']))) { unset($query['fields']); } } + if (empty($query['fields'])) { $query['fields'] = $db->calculate($this, 'count'); } elseif (method_exists($db, 'expression') && is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) { @@ -2876,6 +3015,7 @@ class Model extends Object implements CakeEventListener { $db->expression($query['fields']), 'count' )); } + return $query; } @@ -2884,9 +3024,11 @@ class Model extends Object implements CakeEventListener { if ($query['group']) { return count($results); } + return intval($results[0][$key]['count']); } } + return false; } @@ -2934,16 +3076,19 @@ class Model extends Object implements CakeEventListener { $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; } if (empty($results)) { return array(); } + return Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']); } @@ -2969,11 +3114,13 @@ class Model extends Object implements CakeEventListener { $field = $this->alias . '.' . $this->primaryKey; $value = $this->id; } + $query['conditions'] = array_merge($conditions, array($field . ' <' => $value)); $query['order'] = $field . ' DESC'; $query['limit'] = 1; $query['field'] = $field; $query['value'] = $value; + return $query; } @@ -2989,11 +3136,13 @@ class Model extends Object implements CakeEventListener { $query['conditions'][$field . ' >'] = $value; $query['limit'] = 1; } + $query['order'] = $field . ' ASC'; $neighbors = $this->find('all', $query); if (!array_key_exists('prev', $return)) { $return['prev'] = isset($neighbors[0]) ? $neighbors[0] : null; } + if (count($neighbors) === 2) { $return['next'] = $neighbors[1]; } elseif (count($neighbors) === 1 && !$return['prev']) { @@ -3001,6 +3150,7 @@ class Model extends Object implements CakeEventListener { } else { $return['next'] = null; } + return $return; } @@ -3022,6 +3172,7 @@ class Model extends Object implements CakeEventListener { if (isset($query['parent'])) { $parent = $query['parent']; } + return Hash::nest($results, array( 'idPath' => '{n}.' . $this->alias . '.' . $this->primaryKey, 'parentPath' => '{n}.' . $this->alias . '.' . $parent @@ -3056,6 +3207,7 @@ class Model extends Object implements CakeEventListener { $this->{$type} = $this->__backAssociation[$type]; } } + $this->__backAssociation = array(); } @@ -3066,6 +3218,7 @@ class Model extends Object implements CakeEventListener { } } } + $this->__backAssociation = array(); return true; } @@ -3102,12 +3255,15 @@ class Model extends Object implements CakeEventListener { $fields[$this->alias . '.' . $field] = $value; } } + if ($or) { $fields = array('or' => $fields); } + if (!empty($this->id)) { $fields[$this->alias . '.' . $this->primaryKey . ' !='] = $this->id; } + return !$this->find('count', array('conditions' => $fields, 'recursive' => -1)); } @@ -3178,6 +3334,7 @@ class Model extends Object implements CakeEventListener { $foreignKeys[] = $data['foreignKey']; } } + return in_array($field, $foreignKeys); } @@ -3193,13 +3350,16 @@ class Model extends Object implements CakeEventListener { if (empty($alias)) { $alias = $this->alias; } + if (empty($field)) { $field = $this->primaryKey; } + $db = $this->getDataSource(); if (strpos($field, $db->name($alias) . '.') === 0) { return $field; } + return $db->name($alias . '.' . $field); } @@ -3213,12 +3373,15 @@ class Model extends Object implements CakeEventListener { if (empty($this->id) || (is_array($this->id) && isset($this->id[0]) && empty($this->id[0]))) { return false; } + if (!is_array($this->id)) { return $this->id; } + if (isset($this->id[$list]) && !empty($this->id[$list])) { return $this->id[$list]; } + if (isset($this->id[$list])) { return false; } @@ -3285,6 +3448,7 @@ class Model extends Object implements CakeEventListener { if ($dataSource) { $this->useDbConfig = $dataSource; } + $db = ConnectionManager::getDataSource($this->useDbConfig); if (!empty($oldConfig) && isset($db->config['prefix'])) { $oldDb = ConnectionManager::getDataSource($oldConfig); @@ -3309,6 +3473,7 @@ class Model extends Object implements CakeEventListener { $this->_sourceConfigured = true; $this->setSource($this->useTable); } + return ConnectionManager::getDataSource($this->useDbConfig); } @@ -3338,12 +3503,15 @@ class Model extends Object implements CakeEventListener { } } } + return $associated; } + if (in_array($type, $this->_associations)) { if (empty($this->{$type})) { return array(); } + return array_keys($this->{$type}); } @@ -3353,6 +3521,7 @@ class Model extends Object implements CakeEventListener { $this->belongsTo, $this->hasAndBelongsToMany ); + if (array_key_exists($type, $assoc)) { foreach ($this->_associations as $a) { if (isset($this->{$a}[$type])) { @@ -3360,8 +3529,10 @@ class Model extends Object implements CakeEventListener { break; } } + return $assoc[$type]; } + return null; } @@ -3378,10 +3549,12 @@ class Model extends Object implements CakeEventListener { list(, $assoc) = pluginSplit($assoc); return array($assoc, array_keys($this->{$assoc}->schema())); } + if (is_array($assoc)) { $with = key($assoc); return array($with, array_unique(array_merge($assoc[$with], $keys))); } + trigger_error( __d('cake_dev', 'Invalid join model settings in %s. The association parameter has the wrong type, expecting a string or array, but was passed type: %s', $this->alias, gettype($assoc)), E_USER_WARNING @@ -3527,6 +3700,7 @@ class Model extends Object implements CakeEventListener { } elseif (!$this->_validator) { $this->_validator = new ModelValidator($this); } + return $this->_validator; }