mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Optimizing various finds when deleting records and updating counter cache to avoid extra queries, unnecessary joins and callbacks triggering
This commit is contained in:
parent
182b13fdbd
commit
2b342d96d6
1 changed files with 17 additions and 6 deletions
|
@ -1546,8 +1546,8 @@ class Model extends Object {
|
|||
if (!array_key_exists($foreignKey, $keys)) {
|
||||
$keys[$foreignKey] = $this->field($foreignKey);
|
||||
}
|
||||
$recursive = (isset($assoc['counterScope']) ? 1 : -1);
|
||||
$conditions = ($recursive == 1) ? (array)$assoc['counterScope'] : array();
|
||||
$recursive = (isset($assoc['counterScope']) ? 0 : -1);
|
||||
$conditions = ($recursive === 0) ? (array)$assoc['counterScope'] : array();
|
||||
|
||||
if (isset($keys['old'][$foreignKey])) {
|
||||
if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
|
||||
|
@ -1562,7 +1562,7 @@ class Model extends Object {
|
|||
}
|
||||
$conditions[$fkQuoted] = $keys[$foreignKey];
|
||||
|
||||
if ($recursive == 1) {
|
||||
if ($recursive === 0) {
|
||||
$conditions = array_merge($conditions, (array)$assoc['counterScope']);
|
||||
}
|
||||
$count = intval($this->find('count', compact('conditions', 'recursive')));
|
||||
|
@ -1960,15 +1960,25 @@ class Model extends Object {
|
|||
$this->_deleteLinks($id);
|
||||
$this->id = $id;
|
||||
|
||||
$updateCounterCache = false;
|
||||
if (!empty($this->belongsTo)) {
|
||||
foreach ($this->belongsTo as $parent => $assoc) {
|
||||
if (!empty($assoc['counterCache'])) {
|
||||
$updateCounterCache = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$keys = $this->find('first', array(
|
||||
'fields' => $this->__collectForeignKeys(),
|
||||
'conditions' => array($this->alias . '.' . $this->primaryKey => $id)
|
||||
'conditions' => array($this->alias . '.' . $this->primaryKey => $id),
|
||||
'recursive' => -1,
|
||||
'callbacks' => false
|
||||
));
|
||||
}
|
||||
|
||||
if ($db->delete($this, array($this->alias . '.' . $this->primaryKey => $id))) {
|
||||
if (!empty($this->belongsTo)) {
|
||||
if ($updateCounterCache) {
|
||||
$this->updateCounterCache($keys[$this->alias]);
|
||||
}
|
||||
$this->Behaviors->trigger('afterDelete', array(&$this));
|
||||
|
@ -2035,7 +2045,8 @@ class Model extends Object {
|
|||
$records = $this->{$joinModel}->find('all', array(
|
||||
'conditions' => array_merge(array($this->{$joinModel}->escapeField($data['foreignKey']) => $id)),
|
||||
'fields' => $this->{$joinModel}->primaryKey,
|
||||
'recursive' => -1
|
||||
'recursive' => -1,
|
||||
'callbacks' => false
|
||||
));
|
||||
if (!empty($records)) {
|
||||
foreach ($records as $record) {
|
||||
|
|
Loading…
Reference in a new issue