From 9e8ae66448af49ff1f5c291add55095feee5707d Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Thu, 9 Oct 2014 13:32:50 +0200 Subject: [PATCH 1/2] Adds test to prove that there is an error when a lambda is used for updating counterCache --- .../Behavior/CounterCacheBehaviorTest.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php b/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php index 47b1e284cb6..5fb995b532e 100644 --- a/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php @@ -257,6 +257,40 @@ public function testLambdaNumber() { $this->assertEquals(2, $after->get('posts_published')); } +/** + * Testing counter cache with lambda returning number and changing of related ID + * + * @return void + */ + public function testLambdaNumberUpdate() { + $this->post->belongsTo('Users'); + + $table = $this->post; + $entity = $this->_getEntity(); + + $this->post->addBehavior('CounterCache', [ + 'Users' => [ + 'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) { + $this->assertSame($orgTable, $table); + $this->assertSame($orgEntity, $entity); + + return 2; + } + ] + ]); + + $this->post->save($entity); + $between = $this->_getUser(); + $entity->user_id = 2; + $this->post->save($entity); + $afterUser1 = $this->_getUser(1); + $afterUser2 = $this->_getUser(2); + + $this->assertEquals(2, $between->get('posts_published')); + $this->assertEquals(1, $afterUser1->get('posts_published')); + $this->assertEquals(2, $afterUser2->get('posts_published')); + } + /** * Testing counter cache with lambda returning subqueryn * From b2a3d74804bb8c714a38474257cfbb0c4e7f3f99 Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Thu, 9 Oct 2014 13:57:10 +0200 Subject: [PATCH 2/2] Prevent counterCache error when updating foreignkey with lambda counter --- src/Model/Behavior/CounterCacheBehavior.php | 8 ++++++-- .../TestCase/Model/Behavior/CounterCacheBehaviorTest.php | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Model/Behavior/CounterCacheBehavior.php b/src/Model/Behavior/CounterCacheBehavior.php index 2acc6688ce0..982cdeb13ab 100644 --- a/src/Model/Behavior/CounterCacheBehavior.php +++ b/src/Model/Behavior/CounterCacheBehavior.php @@ -165,7 +165,7 @@ 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); } @@ -173,7 +173,11 @@ protected function _processAssociation(Event $event, Entity $entity, Association $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); } } diff --git a/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php b/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php index 5fb995b532e..edd0ed463c6 100644 --- a/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/CounterCacheBehaviorTest.php @@ -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; + } } ] ]);