Skip to content

Commit

Permalink
Prevent counterCache error when updating foreignkey with lambda counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Oct 9, 2014
1 parent 9e8ae66 commit b2a3d74
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Model/Behavior/CounterCacheBehavior.php
Expand Up @@ -165,15 +165,19 @@ protected function _processAssociation(Event $event, Entity $entity, Association
}

if (!is_string($config) && is_callable($config)) {
$count = $config($event, $entity, $this->_table);
$count = $config($event, $entity, $this->_table, false);
} else {
$count = $this->_getCount($config, $countConditions);
}

$assoc->target()->updateAll([$field => $count], $updateConditions);

if (isset($updateOriginalConditions)) {
$count = $this->_getCount($config, $countOriginalConditions);
if (!is_string($config) && is_callable($config)) {
$count = $config($event, $entity, $this->_table, true);
} else {
$count = $this->_getCount($config, $countOriginalConditions);
}
$assoc->target()->updateAll([$field => $count], $updateOriginalConditions);
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php
Expand Up @@ -270,11 +270,15 @@ public function testLambdaNumberUpdate() {

$this->post->addBehavior('CounterCache', [
'Users' => [
'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) {
'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable, $original) use ($entity, $table) {
$this->assertSame($orgTable, $table);
$this->assertSame($orgEntity, $entity);

return 2;
if (!$original) {
return 2;
} else {
return 1;
}
}
]
]);
Expand Down

0 comments on commit b2a3d74

Please sign in to comment.