From fe7b6cc16d37c61eaccb686afea1333347ccdb8a Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Mon, 13 Feb 2017 12:17:51 +0200 Subject: [PATCH] Add option to prevent counter cache from updating the counter cache --- src/ORM/Behavior/CounterCacheBehavior.php | 21 ++++++-- .../ORM/Behavior/CounterCacheBehaviorTest.php | 49 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/ORM/Behavior/CounterCacheBehavior.php b/src/ORM/Behavior/CounterCacheBehavior.php index 1b8f0f679ce..607171e7feb 100644 --- a/src/ORM/Behavior/CounterCacheBehavior.php +++ b/src/ORM/Behavior/CounterCacheBehavior.php @@ -105,10 +105,15 @@ class CounterCacheBehavior extends Behavior * * @param \Cake\Event\Event $event The beforeSave event that was fired * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved + * @param \ArrayObject $options The options for the query * @return void */ - public function beforeSave(Event $event, EntityInterface $entity) + public function beforeSave(Event $event, EntityInterface $entity, $options) { + if (isset($options['ignoreCounterCache']) && $options['ignoreCounterCache'] === true) { + return; + } + foreach ($this->_config as $assoc => $settings) { $assoc = $this->_table->association($assoc); foreach ($settings as $field => $config) { @@ -137,10 +142,15 @@ public function beforeSave(Event $event, EntityInterface $entity) * * @param \Cake\Event\Event $event The afterSave event that was fired. * @param \Cake\Datasource\EntityInterface $entity The entity that was saved. + * @param \ArrayObject $options The options for the query * @return void */ - public function afterSave(Event $event, EntityInterface $entity) + public function afterSave(Event $event, EntityInterface $entity, $options) { + if (isset($options['ignoreCounterCache']) && $options['ignoreCounterCache'] === true) { + return; + } + $this->_processAssociations($event, $entity); $this->_ignoreDirty = []; } @@ -152,10 +162,15 @@ public function afterSave(Event $event, EntityInterface $entity) * * @param \Cake\Event\Event $event The afterDelete event that was fired. * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted. + * @param \ArrayObject $options The options for the query * @return void */ - public function afterDelete(Event $event, EntityInterface $entity) + public function afterDelete(Event $event, EntityInterface $entity, $options) { + if (isset($options['ignoreCounterCache']) && $options['ignoreCounterCache'] === true) { + return; + } + $this->_processAssociations($event, $entity); } diff --git a/tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php b/tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php index 203ab932e62..9932c593c9a 100644 --- a/tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php +++ b/tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php @@ -130,6 +130,30 @@ public function testAdd() $this->assertEquals(3, $after->get('post_count')); } + /** + * Testing simple counter caching when adding a record + * + * @return void + */ + public function testAddIgnore() + { + $this->post->belongsTo('Users'); + + $this->post->addBehavior('CounterCache', [ + 'Users' => [ + 'post_count' + ] + ]); + + $before = $this->_getUser(); + $entity = $this->_getEntity(); + $this->post->save($entity, ['ignoreCounterCache' => true]); + $after = $this->_getUser(); + + $this->assertEquals(2, $before->get('post_count')); + $this->assertEquals(2, $after->get('post_count')); + } + /** * Testing simple counter caching when adding a record * @@ -182,6 +206,31 @@ public function testDelete() $this->assertEquals(1, $after->get('post_count')); } + /** + * Testing simple counter caching when deleting a record + * + * @return void + */ + public function testDeleteIgnore() + { + $this->post->belongsTo('Users'); + + $this->post->addBehavior('CounterCache', [ + 'Users' => [ + 'post_count' + ] + ]); + + $before = $this->_getUser(); + $post = $this->post->find('all') + ->first(); + $this->post->delete($post, ['ignoreCounterCache' => true]); + $after = $this->_getUser(); + + $this->assertEquals(2, $before->get('post_count')); + $this->assertEquals(2, $after->get('post_count')); + } + /** * Testing update simple counter caching when updating a record association *