Skip to content

Commit

Permalink
delete will cause counter update only when it really needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke authored and Yosuke Basuke Suzuki committed Sep 7, 2011
1 parent 1b66b79 commit 9a02111
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cake/libs/model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2035,15 +2035,25 @@ public function delete($id = null, $cascade = true) {
$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),
'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));
Expand Down Expand Up @@ -2111,6 +2121,7 @@ protected function _deleteLinks($id) {
'conditions' => array_merge(array($this->{$joinModel}->escapeField($data['foreignKey']) => $id)),
'fields' => $this->{$joinModel}->primaryKey,
'recursive' => -1,
'callbacks' => false
));
if (!empty($records)) {
foreach ($records as $record) {
Expand Down

0 comments on commit 9a02111

Please sign in to comment.