mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Merge pull request #999 from dogmatic69/model-simplify
Simplify some model code
This commit is contained in:
commit
00d178fa28
1 changed files with 208 additions and 216 deletions
|
@ -1234,7 +1234,10 @@ class Model extends Object implements CakeEventListener {
|
|||
|
||||
$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']) ||
|
||||
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 $data;
|
||||
}
|
||||
|
||||
|
@ -1321,15 +1323,14 @@ class Model extends Object implements CakeEventListener {
|
|||
$this->_schema = array();
|
||||
}
|
||||
}
|
||||
if (is_string($field)) {
|
||||
if (!is_string($field)) {
|
||||
return $this->_schema;
|
||||
}
|
||||
if (isset($this->_schema[$field])) {
|
||||
return $this->_schema[$field];
|
||||
} else {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return $this->_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an associative array of field names and column types.
|
||||
|
@ -1394,11 +1395,9 @@ class Model extends Object implements CakeEventListener {
|
|||
return false;
|
||||
}
|
||||
|
||||
if ($checkVirtual && !empty($this->virtualFields)) {
|
||||
if ($this->isVirtualField($name)) {
|
||||
if ($checkVirtual && !empty($this->virtualFields) && $this->isVirtualField($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->_schema)) {
|
||||
$this->schema();
|
||||
|
@ -1421,10 +1420,7 @@ class Model extends Object implements CakeEventListener {
|
|||
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 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
if ($this->isVirtualField($field)) {
|
||||
if (strpos($field, '.') !== false) {
|
||||
list($model, $field) = explode('.', $field);
|
||||
list(, $field) = pluginSplit($field);
|
||||
}
|
||||
return $this->virtualFields[$field];
|
||||
}
|
||||
|
@ -1548,13 +1544,15 @@ 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;
|
||||
} else {
|
||||
$recursive = $this->recursive;
|
||||
}
|
||||
$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 (isset($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) {
|
||||
return array_shift($data[0]);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1588,10 +1583,9 @@ class Model extends Object implements CakeEventListener {
|
|||
$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 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
|
||||
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 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
|
||||
if (!$db->create($this, $fields, $values)) {
|
||||
$success = $created = false;
|
||||
$success = false;
|
||||
} else {
|
||||
$created = true;
|
||||
}
|
||||
|
@ -2121,9 +2114,8 @@ class Model extends Object implements CakeEventListener {
|
|||
if ($validates) {
|
||||
if ($transactionBegun) {
|
||||
return $db->commit() !== false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$db->rollback();
|
||||
return false;
|
||||
|
@ -2306,9 +2298,8 @@ class Model extends Object implements CakeEventListener {
|
|||
if ($validates) {
|
||||
if ($transactionBegun) {
|
||||
return $db->commit() !== false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$db->rollback();
|
||||
return false;
|
||||
|
@ -2390,36 +2381,38 @@ class Model extends Object implements CakeEventListener {
|
|||
$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 ($event->isStopped()) {
|
||||
return false;
|
||||
}
|
||||
if (!$this->exists()) {
|
||||
return false;
|
||||
}
|
||||
$db = $this->getDataSource();
|
||||
|
||||
$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 (empty($assoc['counterCache'])) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ($updateCounterCache) {
|
||||
|
||||
$keys = $this->find('first', array(
|
||||
'fields' => $this->_collectForeignKeys(),
|
||||
'conditions' => array($this->alias . '.' . $this->primaryKey => $id),
|
||||
'recursive' => -1,
|
||||
'callbacks' => false
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($db->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) {
|
||||
if ($updateCounterCache) {
|
||||
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));
|
||||
|
@ -2427,9 +2420,6 @@ class Model extends Object implements CakeEventListener {
|
|||
$this->id = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cascades model deletes through associated hasMany and hasOne child records.
|
||||
|
@ -2439,13 +2429,18 @@ class Model extends Object implements CakeEventListener {
|
|||
* @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) {
|
||||
if ($data['dependent'] !== true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$model = $this->{$assoc};
|
||||
|
||||
|
@ -2474,8 +2469,6 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($savedAssociatons)) {
|
||||
$this->__backAssociation = $savedAssociatons;
|
||||
}
|
||||
|
@ -2489,7 +2482,7 @@ class Model extends Object implements CakeEventListener {
|
|||
*/
|
||||
protected function _deleteLinks($id) {
|
||||
foreach ($this->hasAndBelongsToMany as $data) {
|
||||
list($plugin, $joinModel) = pluginSplit($data['with']);
|
||||
list(, $joinModel) = pluginSplit($data['with']);
|
||||
$records = $this->{$joinModel}->find('all', array(
|
||||
'conditions' => array($this->{$joinModel}->escapeField($data['foreignKey']) => $id),
|
||||
'fields' => $this->{$joinModel}->primaryKey,
|
||||
|
@ -2521,7 +2514,7 @@ class Model extends Object implements CakeEventListener {
|
|||
|
||||
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'))
|
||||
|
@ -2539,11 +2532,12 @@ class Model extends Object implements CakeEventListener {
|
|||
$_id = $this->id;
|
||||
$result = true;
|
||||
foreach ($ids as $id) {
|
||||
$result = ($result && $this->delete($id, $cascade));
|
||||
$result = $result && $this->delete($id, $cascade);
|
||||
}
|
||||
$this->id = $_id;
|
||||
return $result;
|
||||
} else {
|
||||
}
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$this->_deleteLinks($id);
|
||||
if ($cascade) {
|
||||
|
@ -2552,8 +2546,6 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
return $db->delete($this, array($this->alias . '.' . $this->primaryKey => $ids));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects foreign keys from associations.
|
||||
|
@ -2589,9 +2581,13 @@ class Model extends Object implements CakeEventListener {
|
|||
if ($id === false) {
|
||||
return false;
|
||||
}
|
||||
$conditions = array($this->alias . '.' . $this->primaryKey => $id);
|
||||
$query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false);
|
||||
return ($this->find('count', $query) > 0);
|
||||
return (bool)$this->find('count', array(
|
||||
'conditions' => array(
|
||||
$this->alias . '.' . $this->primaryKey => $id
|
||||
),
|
||||
'recursive' => -1,
|
||||
'callbacks' => false
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2704,11 +2700,9 @@ class Model extends Object implements CakeEventListener {
|
|||
(array)$query
|
||||
);
|
||||
|
||||
if ($type !== 'all') {
|
||||
if ($this->findMethods[$type] === true) {
|
||||
if ($type !== 'all' && $this->findMethods[$type] === true) {
|
||||
$query = $this->{'_find' . ucfirst($type)}('before', $query);
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_numeric($query['page']) || intval($query['page']) < 1) {
|
||||
$query['page'] = 1;
|
||||
|
@ -2793,9 +2787,8 @@ class Model extends Object implements CakeEventListener {
|
|||
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 +2848,7 @@ class Model extends Object implements CakeEventListener {
|
|||
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 +2996,9 @@ class Model extends Object implements CakeEventListener {
|
|||
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 +3114,13 @@ 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];
|
||||
} elseif (isset($this->id[$list])) {
|
||||
}
|
||||
if (isset($this->id[$list])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -3250,12 +3240,14 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
}
|
||||
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,
|
||||
|
@ -3273,7 +3265,6 @@ class Model extends Object implements CakeEventListener {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
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)));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue