diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 8a67de012ec..5066dde2d93 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1234,72 +1234,74 @@ public function deconstruct($field, $data) { $type = $this->getColumnType($field); - if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) { - $useNewDate = (isset($data['year']) || isset($data['month']) || - isset($data['day']) || isset($data['hour']) || isset($data['minute'])); + if (!in_array($type, array('datetime', 'timestamp', 'date', 'time'))) { + return $data; + } - $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); - $timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec'); - $date = array(); + $useNewDate = (isset($data['year']) || isset($data['month']) || + isset($data['day']) || isset($data['hour']) || isset($data['minute'])); - if (isset($data['meridian']) && empty($data['meridian'])) { - return null; - } + $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); + $timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec'); + $date = array(); - if ( - isset($data['hour']) && - isset($data['meridian']) && - !empty($data['hour']) && - $data['hour'] != 12 && - 'pm' == $data['meridian'] - ) { - $data['hour'] = $data['hour'] + 12; - } - if (isset($data['hour']) && isset($data['meridian']) && $data['hour'] == 12 && 'am' == $data['meridian']) { - $data['hour'] = '00'; + if (isset($data['meridian']) && empty($data['meridian'])) { + return null; + } + + if ( + isset($data['hour']) && + isset($data['meridian']) && + !empty($data['hour']) && + $data['hour'] != 12 && + 'pm' == $data['meridian'] + ) { + $data['hour'] = $data['hour'] + 12; + } + if (isset($data['hour']) && isset($data['meridian']) && $data['hour'] == 12 && 'am' == $data['meridian']) { + $data['hour'] = '00'; + } + if ($type == 'time') { + foreach ($timeFields as $key => $val) { + if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') { + $data[$val] = '00'; + } elseif ($data[$val] !== '') { + $data[$val] = sprintf('%02d', $data[$val]); + } + if (!empty($data[$val])) { + $date[$key] = $data[$val]; + } else { + return null; + } } - if ($type == 'time') { - foreach ($timeFields as $key => $val) { + } + + if ($type == 'datetime' || $type == 'timestamp' || $type == 'date') { + foreach ($dateFields as $key => $val) { + if ($val == 'hour' || $val == 'min' || $val == 'sec') { if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') { $data[$val] = '00'; - } elseif ($data[$val] !== '') { - $data[$val] = sprintf('%02d', $data[$val]); - } - if (!empty($data[$val])) { - $date[$key] = $data[$val]; } else { - return null; + $data[$val] = sprintf('%02d', $data[$val]); } } - } - - if ($type == 'datetime' || $type == 'timestamp' || $type == 'date') { - foreach ($dateFields as $key => $val) { - if ($val == 'hour' || $val == 'min' || $val == 'sec') { - if (!isset($data[$val]) || $data[$val] === '0' || $data[$val] === '00') { - $data[$val] = '00'; - } else { - $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]; - } + 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]; } } + } - if ($useNewDate && !empty($date)) { - $format = $this->getDataSource()->columns[$type]['format']; - foreach (array('m', 'd', 'H', 'i', 's') as $index) { - if (isset($date[$index])) { - $date[$index] = sprintf('%02d', $date[$index]); - } + if ($useNewDate && !empty($date)) { + $format = $this->getDataSource()->columns[$type]['format']; + foreach (array('m', 'd', 'H', 'i', 's') as $index) { + if (isset($date[$index])) { + $date[$index] = sprintf('%02d', $date[$index]); } - return str_replace(array_keys($date), array_values($date), $format); } + return str_replace(array_keys($date), array_values($date), $format); } return $data; } @@ -1321,14 +1323,13 @@ public function schema($field = false) { $this->_schema = array(); } } - if (is_string($field)) { - if (isset($this->_schema[$field])) { - return $this->_schema[$field]; - } else { - return null; - } + if (!is_string($field)) { + return $this->_schema; + } + if (isset($this->_schema[$field])) { + return $this->_schema[$field]; } - return $this->_schema; + return null; } /** @@ -1394,10 +1395,8 @@ public function hasField($name, $checkVirtual = false) { return false; } - if ($checkVirtual && !empty($this->virtualFields)) { - if ($this->isVirtualField($name)) { - return true; - } + if ($checkVirtual && !empty($this->virtualFields) && $this->isVirtualField($name)) { + return true; } if (empty($this->_schema)) { @@ -1421,10 +1420,7 @@ public function hasMethod($method) { if (method_exists($this, $method)) { return true; } - if ($this->Behaviors->hasMethod($method)) { - return true; - } - return false; + return $this->Behaviors->hasMethod($method); } /** @@ -1463,7 +1459,7 @@ public function getVirtualField($field = null) { } if ($this->isVirtualField($field)) { if (strpos($field, '.') !== false) { - list($model, $field) = explode('.', $field); + list(, $field) = pluginSplit($field); } return $this->virtualFields[$field]; } @@ -1548,28 +1544,27 @@ public function field($name, $conditions = null, $order = null) { if ($conditions === null && $this->id !== false) { $conditions = array($this->alias . '.' . $this->primaryKey => $this->id); } + $recursive = $this->recursive; if ($this->recursive >= 1) { $recursive = -1; - } else { - $recursive = $this->recursive; } $fields = $name; - if ($data = $this->find('first', compact('conditions', 'fields', 'order', 'recursive'))) { - if (strpos($name, '.') === false) { - if (isset($data[$this->alias][$name])) { - return $data[$this->alias][$name]; - } - } else { - $name = explode('.', $name); - if (isset($data[$name[0]][$name[1]])) { - return $data[$name[0]][$name[1]]; - } - } - if (isset($data[0]) && count($data[0]) > 0) { - return array_shift($data[0]); + $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]; } } else { - return false; + $name = explode('.', $name); + if (isset($data[$name[0]][$name[1]])) { + return $data[$name[0]][$name[1]]; + } + } + if (isset($data[0]) && count($data[0]) > 0) { + return array_shift($data[0]); } } @@ -1588,10 +1583,9 @@ public function saveField($name, $value, $validate = false) { $id = $this->id; $this->create(false); + $options = array('validate' => $validate, 'fieldList' => array($name)); if (is_array($validate)) { $options = array_merge(array('validate' => false, 'fieldList' => array($name)), $validate); - } else { - $options = array('validate' => $validate, 'fieldList' => array($name)); } return $this->save(array($this->alias => array($this->primaryKey => $id, $name => $value)), $options); } @@ -1620,10 +1614,9 @@ public function save($data = null, $validate = true, $fieldList = array()) { } if (!empty($options['fieldList'])) { + $this->whitelist = $options['fieldList']; if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) { $this->whitelist = $options['fieldList'][$this->alias]; - } else { - $this->whitelist = $options['fieldList']; } } elseif ($options['fieldList'] === null) { $this->whitelist = array(); @@ -1742,7 +1735,7 @@ public function save($data = null, $validate = true, $fieldList = array()) { } if (!$db->create($this, $fields, $values)) { - $success = $created = false; + $success = false; } else { $created = true; } @@ -2121,9 +2114,8 @@ public function saveMany($data = null, $options = array()) { if ($validates) { if ($transactionBegun) { return $db->commit() !== false; - } else { - return true; } + return true; } $db->rollback(); return false; @@ -2306,9 +2298,8 @@ public function saveAssociated($data = null, $options = array()) { if ($validates) { if ($transactionBegun) { return $db->commit() !== false; - } else { - return true; } + return true; } $db->rollback(); return false; @@ -2390,45 +2381,44 @@ public function delete($id = null, $cascade = true) { $event = new CakeEvent('Model.beforeDelete', $this, array($cascade)); list($event->break, $event->breakOn) = array(true, array(false, null)); $this->getEventManager()->dispatch($event); - if (!$event->isStopped()) { - if (!$this->exists()) { - return false; - } - $db = $this->getDataSource(); + if ($event->isStopped()) { + return false; + } + if (!$this->exists()) { + return false; + } - $this->_deleteDependent($id, $cascade); - $this->_deleteLinks($id); - $this->id = $id; + $this->_deleteDependent($id, $cascade); + $this->_deleteLinks($id); + $this->id = $id; - $updateCounterCache = false; - if (!empty($this->belongsTo)) { - foreach ($this->belongsTo as $assoc) { - if (!empty($assoc['counterCache'])) { - $updateCounterCache = true; - break; - } - } - if ($updateCounterCache) { - $keys = $this->find('first', array( - 'fields' => $this->_collectForeignKeys(), - 'conditions' => array($this->alias . '.' . $this->primaryKey => $id), - 'recursive' => -1, - 'callbacks' => false - )); + if (!empty($this->belongsTo)) { + foreach ($this->belongsTo as $assoc) { + if (empty($assoc['counterCache'])) { + continue; } - } - if ($db->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) { - if ($updateCounterCache) { - $this->updateCounterCache($keys[$this->alias]); - } - $this->getEventManager()->dispatch(new CakeEvent('Model.afterDelete', $this)); - $this->_clearCache(); - $this->id = false; - return true; + $keys = $this->find('first', array( + 'fields' => $this->_collectForeignKeys(), + 'conditions' => array($this->alias . '.' . $this->primaryKey => $id), + 'recursive' => -1, + 'callbacks' => false + )); + break; } } - return false; + + if (!$this->getDataSource()->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) { + return false; + } + + 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; } /** @@ -2439,38 +2429,39 @@ public function delete($id = null, $cascade = true) { * @return void */ protected function _deleteDependent($id, $cascade) { + if ($cascade !== true) { + return; + } if (!empty($this->__backAssociation)) { $savedAssociatons = $this->__backAssociation; $this->__backAssociation = array(); } - if ($cascade === true) { - foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) { - if ($data['dependent'] === true) { - $model = $this->{$assoc}; + foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) { + if ($data['dependent'] === true) { + $model = $this->{$assoc}; - if ($data['foreignKey'] === false && $data['conditions'] && in_array($this->name, $model->getAssociated('belongsTo'))) { - $model->recursive = 0; - $conditions = array($this->escapeField(null, $this->name) => $id); - } else { - $model->recursive = -1; - $conditions = array($model->escapeField($data['foreignKey']) => $id); - if ($data['conditions']) { - $conditions = array_merge((array)$data['conditions'], $conditions); - } + if ($data['foreignKey'] === false && $data['conditions'] && in_array($this->name, $model->getAssociated('belongsTo'))) { + $model->recursive = 0; + $conditions = array($this->escapeField(null, $this->name) => $id); + } else { + $model->recursive = -1; + $conditions = array($model->escapeField($data['foreignKey']) => $id); + if ($data['conditions']) { + $conditions = array_merge((array)$data['conditions'], $conditions); } + } - if (isset($data['exclusive']) && $data['exclusive']) { - $model->deleteAll($conditions); - } else { - $records = $model->find('all', array( - 'conditions' => $conditions, 'fields' => $model->primaryKey - )); + if (isset($data['exclusive']) && $data['exclusive']) { + $model->deleteAll($conditions); + } else { + $records = $model->find('all', array( + 'conditions' => $conditions, 'fields' => $model->primaryKey + )); - if (!empty($records)) { - foreach ($records as $record) { - $model->delete($record[$model->alias][$model->primaryKey]); - } + if (!empty($records)) { + foreach ($records as $record) { + $model->delete($record[$model->alias][$model->primaryKey]); } } } @@ -2521,38 +2512,37 @@ public function deleteAll($conditions, $cascade = true, $callbacks = false) { if (!$cascade && !$callbacks) { return $db->delete($this, $conditions); - } else { - $ids = $this->find('all', array_merge(array( - 'fields' => "{$this->alias}.{$this->primaryKey}", - 'recursive' => 0), compact('conditions')) - ); - if ($ids === false) { - return false; - } + } + $ids = $this->find('all', array_merge(array( + 'fields' => "{$this->alias}.{$this->primaryKey}", + 'recursive' => 0), compact('conditions')) + ); + if ($ids === false) { + return false; + } - $ids = Hash::extract($ids, "{n}.{$this->alias}.{$this->primaryKey}"); - if (empty($ids)) { - return true; + $ids = Hash::extract($ids, "{n}.{$this->alias}.{$this->primaryKey}"); + if (empty($ids)) { + return true; + } + + if ($callbacks) { + $_id = $this->id; + $result = true; + foreach ($ids as $id) { + $result = $result && $this->delete($id, $cascade); } + $this->id = $_id; + return $result; + } - if ($callbacks) { - $_id = $this->id; - $result = true; - foreach ($ids as $id) { - $result = ($result && $this->delete($id, $cascade)); - } - $this->id = $_id; - return $result; - } else { - foreach ($ids as $id) { - $this->_deleteLinks($id); - if ($cascade) { - $this->_deleteDependent($id, $cascade); - } - } - return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids)); + foreach ($ids as $id) { + $this->_deleteLinks($id); + if ($cascade) { + $this->_deleteDependent($id, $cascade); } } + return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids)); } /** @@ -2591,7 +2581,7 @@ public function exists($id = null) { } $conditions = array($this->alias . '.' . $this->primaryKey => $id); $query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false); - return ($this->find('count', $query) > 0); + return $this->find('count', $query) > 0; } /** @@ -2704,10 +2694,8 @@ public function buildQuery($type = 'first', $query = array()) { (array)$query ); - if ($type !== 'all') { - if ($this->findMethods[$type] === true) { - $query = $this->{'_find' . ucfirst($type)}('before', $query); - } + if ($type !== 'all' && $this->findMethods[$type] === true) { + $query = $this->{'_find' . ucfirst($type)}('before', $query); } if (!is_numeric($query['page']) || intval($query['page']) < 1) { @@ -2793,9 +2781,8 @@ protected function _findCount($state, $query, $results = array()) { if (isset($results[0][$key]['count'])) { if ($query['group']) { return count($results); - } else { - return intval($results[0][$key]['count']); } + return intval($results[0][$key]['count']); } } return false; @@ -2855,8 +2842,7 @@ protected function _findList($state, $query, $results = array()) { if (empty($results)) { return array(); } - $lst = $query['list']; - return Hash::combine($results, $lst['keyPath'], $lst['valuePath'], $lst['groupPath']); + return Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']); } } @@ -3004,10 +2990,9 @@ public function isUnique($fields, $or = true) { unset($fields[$field]); $field = $value; + $value = null; if (isset($this->data[$this->alias][$field])) { $value = $this->data[$this->alias][$field]; - } else { - $value = null; } } @@ -3123,14 +3108,13 @@ public function getID($list = 0) { 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]; - } elseif (isset($this->id[$list])) { + } + if (isset($this->id[$list])) { return false; } @@ -3250,29 +3234,30 @@ public function getAssociated($type = null) { } } return $associated; - } elseif (in_array($type, $this->_associations)) { + } + if (in_array($type, $this->_associations)) { if (empty($this->{$type})) { return array(); } return array_keys($this->{$type}); - } else { - $assoc = array_merge( - $this->hasOne, - $this->hasMany, - $this->belongsTo, - $this->hasAndBelongsToMany - ); - if (array_key_exists($type, $assoc)) { - foreach ($this->_associations as $a) { - if (isset($this->{$a}[$type])) { - $assoc[$type]['association'] = $a; - break; - } + } + + $assoc = array_merge( + $this->hasOne, + $this->hasMany, + $this->belongsTo, + $this->hasAndBelongsToMany + ); + if (array_key_exists($type, $assoc)) { + foreach ($this->_associations as $a) { + if (isset($this->{$a}[$type])) { + $assoc[$type]['association'] = $a; + break; } - return $assoc[$type]; } - return null; + return $assoc[$type]; } + return null; } /** @@ -3287,7 +3272,8 @@ public function joinModel($assoc, $keys = array()) { if (is_string($assoc)) { list(, $assoc) = pluginSplit($assoc); return array($assoc, array_keys($this->{$assoc}->schema())); - } elseif (is_array($assoc)) { + } + if (is_array($assoc)) { $with = key($assoc); return array($with, array_unique(array_merge($assoc[$with], $keys))); }