Skip to content

Commit

Permalink
Add option to prevent counter cache from updating the counter cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota committed Feb 13, 2017
1 parent 35b2390 commit fe7b6cc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/ORM/Behavior/CounterCacheBehavior.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = [];
}
Expand All @@ -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);
}

Expand Down
49 changes: 49 additions & 0 deletions tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit fe7b6cc

Please sign in to comment.