Merge pull request #999 from dogmatic69/model-simplify

Simplify some model code
This commit is contained in:
Mark Story 2012-12-05 18:18:50 -08:00
commit 00d178fa28

View file

@ -1234,7 +1234,10 @@ class Model extends Object implements CakeEventListener {
$type = $this->getColumnType($field); $type = $this->getColumnType($field);
if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) { if (!in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
return $data;
}
$useNewDate = (isset($data['year']) || isset($data['month']) || $useNewDate = (isset($data['year']) || isset($data['month']) ||
isset($data['day']) || isset($data['hour']) || isset($data['minute'])); isset($data['day']) || isset($data['hour']) || isset($data['minute']));
@ -1300,7 +1303,6 @@ class Model extends Object implements CakeEventListener {
} }
return str_replace(array_keys($date), array_values($date), $format); return str_replace(array_keys($date), array_values($date), $format);
} }
}
return $data; return $data;
} }
@ -1321,15 +1323,14 @@ class Model extends Object implements CakeEventListener {
$this->_schema = array(); $this->_schema = array();
} }
} }
if (is_string($field)) { if (!is_string($field)) {
return $this->_schema;
}
if (isset($this->_schema[$field])) { if (isset($this->_schema[$field])) {
return $this->_schema[$field]; return $this->_schema[$field];
} else { }
return null; return null;
} }
}
return $this->_schema;
}
/** /**
* Returns an associative array of field names and column types. * Returns an associative array of field names and column types.
@ -1394,11 +1395,9 @@ class Model extends Object implements CakeEventListener {
return false; return false;
} }
if ($checkVirtual && !empty($this->virtualFields)) { if ($checkVirtual && !empty($this->virtualFields) && $this->isVirtualField($name)) {
if ($this->isVirtualField($name)) {
return true; return true;
} }
}
if (empty($this->_schema)) { if (empty($this->_schema)) {
$this->schema(); $this->schema();
@ -1421,10 +1420,7 @@ class Model extends Object implements CakeEventListener {
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return true; return true;
} }
if ($this->Behaviors->hasMethod($method)) { return $this->Behaviors->hasMethod($method);
return true;
}
return false;
} }
/** /**
@ -1463,7 +1459,7 @@ class Model extends Object implements CakeEventListener {
} }
if ($this->isVirtualField($field)) { if ($this->isVirtualField($field)) {
if (strpos($field, '.') !== false) { if (strpos($field, '.') !== false) {
list($model, $field) = explode('.', $field); list(, $field) = pluginSplit($field);
} }
return $this->virtualFields[$field]; return $this->virtualFields[$field];
} }
@ -1548,13 +1544,15 @@ class Model extends Object implements CakeEventListener {
if ($conditions === null && $this->id !== false) { if ($conditions === null && $this->id !== false) {
$conditions = array($this->alias . '.' . $this->primaryKey => $this->id); $conditions = array($this->alias . '.' . $this->primaryKey => $this->id);
} }
$recursive = $this->recursive;
if ($this->recursive >= 1) { if ($this->recursive >= 1) {
$recursive = -1; $recursive = -1;
} else {
$recursive = $this->recursive;
} }
$fields = $name; $fields = $name;
if ($data = $this->find('first', compact('conditions', 'fields', 'order', 'recursive'))) { $data = $this->find('first', compact('conditions', 'fields', 'order', 'recursive'));
if (!$data) {
return false;
}
if (strpos($name, '.') === false) { if (strpos($name, '.') === false) {
if (isset($data[$this->alias][$name])) { if (isset($data[$this->alias][$name])) {
return $data[$this->alias][$name]; return $data[$this->alias][$name];
@ -1568,9 +1566,6 @@ class Model extends Object implements CakeEventListener {
if (isset($data[0]) && count($data[0]) > 0) { if (isset($data[0]) && count($data[0]) > 0) {
return array_shift($data[0]); return array_shift($data[0]);
} }
} else {
return false;
}
} }
/** /**
@ -1588,10 +1583,9 @@ class Model extends Object implements CakeEventListener {
$id = $this->id; $id = $this->id;
$this->create(false); $this->create(false);
$options = array('validate' => $validate, 'fieldList' => array($name));
if (is_array($validate)) { if (is_array($validate)) {
$options = array_merge(array('validate' => false, 'fieldList' => array($name)), $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); return $this->save(array($this->alias => array($this->primaryKey => $id, $name => $value)), $options);
} }
@ -1620,10 +1614,9 @@ class Model extends Object implements CakeEventListener {
} }
if (!empty($options['fieldList'])) { if (!empty($options['fieldList'])) {
$this->whitelist = $options['fieldList'];
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) { if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
$this->whitelist = $options['fieldList'][$this->alias]; $this->whitelist = $options['fieldList'][$this->alias];
} else {
$this->whitelist = $options['fieldList'];
} }
} elseif ($options['fieldList'] === null) { } elseif ($options['fieldList'] === null) {
$this->whitelist = array(); $this->whitelist = array();
@ -1742,7 +1735,7 @@ class Model extends Object implements CakeEventListener {
} }
if (!$db->create($this, $fields, $values)) { if (!$db->create($this, $fields, $values)) {
$success = $created = false; $success = false;
} else { } else {
$created = true; $created = true;
} }
@ -2121,9 +2114,8 @@ class Model extends Object implements CakeEventListener {
if ($validates) { if ($validates) {
if ($transactionBegun) { if ($transactionBegun) {
return $db->commit() !== false; return $db->commit() !== false;
} else {
return true;
} }
return true;
} }
$db->rollback(); $db->rollback();
return false; return false;
@ -2306,9 +2298,8 @@ class Model extends Object implements CakeEventListener {
if ($validates) { if ($validates) {
if ($transactionBegun) { if ($transactionBegun) {
return $db->commit() !== false; return $db->commit() !== false;
} else {
return true;
} }
return true;
} }
$db->rollback(); $db->rollback();
return false; return false;
@ -2390,36 +2381,38 @@ class Model extends Object implements CakeEventListener {
$event = new CakeEvent('Model.beforeDelete', $this, array($cascade)); $event = new CakeEvent('Model.beforeDelete', $this, array($cascade));
list($event->break, $event->breakOn) = array(true, array(false, null)); list($event->break, $event->breakOn) = array(true, array(false, null));
$this->getEventManager()->dispatch($event); $this->getEventManager()->dispatch($event);
if (!$event->isStopped()) { if ($event->isStopped()) {
return false;
}
if (!$this->exists()) { if (!$this->exists()) {
return false; return false;
} }
$db = $this->getDataSource();
$this->_deleteDependent($id, $cascade); $this->_deleteDependent($id, $cascade);
$this->_deleteLinks($id); $this->_deleteLinks($id);
$this->id = $id; $this->id = $id;
$updateCounterCache = false;
if (!empty($this->belongsTo)) { if (!empty($this->belongsTo)) {
foreach ($this->belongsTo as $assoc) { foreach ($this->belongsTo as $assoc) {
if (!empty($assoc['counterCache'])) { if (empty($assoc['counterCache'])) {
$updateCounterCache = true; continue;
break;
} }
}
if ($updateCounterCache) {
$keys = $this->find('first', array( $keys = $this->find('first', array(
'fields' => $this->_collectForeignKeys(), 'fields' => $this->_collectForeignKeys(),
'conditions' => array($this->alias . '.' . $this->primaryKey => $id), 'conditions' => array($this->alias . '.' . $this->primaryKey => $id),
'recursive' => -1, 'recursive' => -1,
'callbacks' => false 'callbacks' => false
)); ));
break;
} }
} }
if ($db->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) { if (!$this->getDataSource()->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) {
if ($updateCounterCache) { return false;
}
if (!empty($keys[$this->alias])) {
$this->updateCounterCache($keys[$this->alias]); $this->updateCounterCache($keys[$this->alias]);
} }
$this->getEventManager()->dispatch(new CakeEvent('Model.afterDelete', $this)); $this->getEventManager()->dispatch(new CakeEvent('Model.afterDelete', $this));
@ -2427,9 +2420,6 @@ class Model extends Object implements CakeEventListener {
$this->id = false; $this->id = false;
return true; return true;
} }
}
return false;
}
/** /**
* Cascades model deletes through associated hasMany and hasOne child records. * Cascades model deletes through associated hasMany and hasOne child records.
@ -2439,13 +2429,18 @@ class Model extends Object implements CakeEventListener {
* @return void * @return void
*/ */
protected function _deleteDependent($id, $cascade) { protected function _deleteDependent($id, $cascade) {
if ($cascade !== true) {
return;
}
if (!empty($this->__backAssociation)) { if (!empty($this->__backAssociation)) {
$savedAssociatons = $this->__backAssociation; $savedAssociatons = $this->__backAssociation;
$this->__backAssociation = array(); $this->__backAssociation = array();
} }
if ($cascade === true) {
foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) { foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) {
if ($data['dependent'] === true) { if ($data['dependent'] !== true) {
continue;
}
$model = $this->{$assoc}; $model = $this->{$assoc};
@ -2474,8 +2469,6 @@ class Model extends Object implements CakeEventListener {
} }
} }
} }
}
}
if (isset($savedAssociatons)) { if (isset($savedAssociatons)) {
$this->__backAssociation = $savedAssociatons; $this->__backAssociation = $savedAssociatons;
} }
@ -2489,7 +2482,7 @@ class Model extends Object implements CakeEventListener {
*/ */
protected function _deleteLinks($id) { protected function _deleteLinks($id) {
foreach ($this->hasAndBelongsToMany as $data) { foreach ($this->hasAndBelongsToMany as $data) {
list($plugin, $joinModel) = pluginSplit($data['with']); list(, $joinModel) = pluginSplit($data['with']);
$records = $this->{$joinModel}->find('all', array( $records = $this->{$joinModel}->find('all', array(
'conditions' => array($this->{$joinModel}->escapeField($data['foreignKey']) => $id), 'conditions' => array($this->{$joinModel}->escapeField($data['foreignKey']) => $id),
'fields' => $this->{$joinModel}->primaryKey, 'fields' => $this->{$joinModel}->primaryKey,
@ -2521,7 +2514,7 @@ class Model extends Object implements CakeEventListener {
if (!$cascade && !$callbacks) { if (!$cascade && !$callbacks) {
return $db->delete($this, $conditions); return $db->delete($this, $conditions);
} else { }
$ids = $this->find('all', array_merge(array( $ids = $this->find('all', array_merge(array(
'fields' => "{$this->alias}.{$this->primaryKey}", 'fields' => "{$this->alias}.{$this->primaryKey}",
'recursive' => 0), compact('conditions')) 'recursive' => 0), compact('conditions'))
@ -2539,11 +2532,12 @@ class Model extends Object implements CakeEventListener {
$_id = $this->id; $_id = $this->id;
$result = true; $result = true;
foreach ($ids as $id) { foreach ($ids as $id) {
$result = ($result && $this->delete($id, $cascade)); $result = $result && $this->delete($id, $cascade);
} }
$this->id = $_id; $this->id = $_id;
return $result; return $result;
} else { }
foreach ($ids as $id) { foreach ($ids as $id) {
$this->_deleteLinks($id); $this->_deleteLinks($id);
if ($cascade) { if ($cascade) {
@ -2552,8 +2546,6 @@ class Model extends Object implements CakeEventListener {
} }
return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids)); return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids));
} }
}
}
/** /**
* Collects foreign keys from associations. * Collects foreign keys from associations.
@ -2589,9 +2581,13 @@ class Model extends Object implements CakeEventListener {
if ($id === false) { if ($id === false) {
return false; return false;
} }
$conditions = array($this->alias . '.' . $this->primaryKey => $id); return (bool)$this->find('count', array(
$query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false); 'conditions' => array(
return ($this->find('count', $query) > 0); $this->alias . '.' . $this->primaryKey => $id
),
'recursive' => -1,
'callbacks' => false
));
} }
/** /**
@ -2704,11 +2700,9 @@ class Model extends Object implements CakeEventListener {
(array)$query (array)$query
); );
if ($type !== 'all') { if ($type !== 'all' && $this->findMethods[$type] === true) {
if ($this->findMethods[$type] === true) {
$query = $this->{'_find' . ucfirst($type)}('before', $query); $query = $this->{'_find' . ucfirst($type)}('before', $query);
} }
}
if (!is_numeric($query['page']) || intval($query['page']) < 1) { if (!is_numeric($query['page']) || intval($query['page']) < 1) {
$query['page'] = 1; $query['page'] = 1;
@ -2793,9 +2787,8 @@ class Model extends Object implements CakeEventListener {
if (isset($results[0][$key]['count'])) { if (isset($results[0][$key]['count'])) {
if ($query['group']) { if ($query['group']) {
return count($results); return count($results);
} else {
return intval($results[0][$key]['count']);
} }
return intval($results[0][$key]['count']);
} }
} }
return false; return false;
@ -2855,8 +2848,7 @@ class Model extends Object implements CakeEventListener {
if (empty($results)) { if (empty($results)) {
return array(); return array();
} }
$lst = $query['list']; return Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath']);
return Hash::combine($results, $lst['keyPath'], $lst['valuePath'], $lst['groupPath']);
} }
} }
@ -3004,10 +2996,9 @@ class Model extends Object implements CakeEventListener {
unset($fields[$field]); unset($fields[$field]);
$field = $value; $field = $value;
$value = null;
if (isset($this->data[$this->alias][$field])) { if (isset($this->data[$this->alias][$field])) {
$value = $this->data[$this->alias][$field]; $value = $this->data[$this->alias][$field];
} else {
$value = null;
} }
} }
@ -3123,14 +3114,13 @@ class Model extends Object implements CakeEventListener {
if (empty($this->id) || (is_array($this->id) && isset($this->id[0]) && empty($this->id[0]))) { if (empty($this->id) || (is_array($this->id) && isset($this->id[0]) && empty($this->id[0]))) {
return false; return false;
} }
if (!is_array($this->id)) { if (!is_array($this->id)) {
return $this->id; return $this->id;
} }
if (isset($this->id[$list]) && !empty($this->id[$list])) { if (isset($this->id[$list]) && !empty($this->id[$list])) {
return $this->id[$list]; return $this->id[$list];
} elseif (isset($this->id[$list])) { }
if (isset($this->id[$list])) {
return false; return false;
} }
@ -3250,12 +3240,14 @@ class Model extends Object implements CakeEventListener {
} }
} }
return $associated; return $associated;
} elseif (in_array($type, $this->_associations)) { }
if (in_array($type, $this->_associations)) {
if (empty($this->{$type})) { if (empty($this->{$type})) {
return array(); return array();
} }
return array_keys($this->{$type}); return array_keys($this->{$type});
} else { }
$assoc = array_merge( $assoc = array_merge(
$this->hasOne, $this->hasOne,
$this->hasMany, $this->hasMany,
@ -3273,7 +3265,6 @@ class Model extends Object implements CakeEventListener {
} }
return null; return null;
} }
}
/** /**
* Gets the name and fields to be used by a join model. This allows specifying join fields * Gets the name and fields to be used by a join model. This allows specifying join fields
@ -3287,7 +3278,8 @@ class Model extends Object implements CakeEventListener {
if (is_string($assoc)) { if (is_string($assoc)) {
list(, $assoc) = pluginSplit($assoc); list(, $assoc) = pluginSplit($assoc);
return array($assoc, array_keys($this->{$assoc}->schema())); return array($assoc, array_keys($this->{$assoc}->schema()));
} elseif (is_array($assoc)) { }
if (is_array($assoc)) {
$with = key($assoc); $with = key($assoc);
return array($with, array_unique(array_merge($assoc[$with], $keys))); return array($with, array_unique(array_merge($assoc[$with], $keys)));
} }