Skip to content
Permalink
Browse files

Use binding key in counter cache.

Ref #7609
  • Loading branch information...
sdustinh committed Oct 29, 2015
1 parent 9d7d63c commit f06c6cff56a39d89e29253d8bd2710842f293fd6
@@ -135,11 +135,11 @@ protected function _processAssociations(Event $event, EntityInterface $entity)
protected function _processAssociation(Event $event, EntityInterface $entity, Association $assoc, array $settings)
{
$foreignKeys = (array)$assoc->foreignKey();
$primaryKeys = (array)$assoc->target()->primaryKey();
$primaryKeys = (array)$assoc->bindingKey();
$countConditions = $entity->extract($foreignKeys);
$updateConditions = array_combine($primaryKeys, $countConditions);
$countOriginalConditions = $entity->extractOriginalChanged($foreignKeys);
if ($countOriginalConditions !== []) {
$updateOriginalConditions = array_combine($primaryKeys, $countOriginalConditions);
}
@@ -0,0 +1,39 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* Short description for class.
*
*/
class CounterCacheUserCategoryPostsFixture extends TestFixture
{
public $fields = [
'id' => ['type' => 'integer'],
'category_id' => ['type' => 'integer'],
'user_id' => ['type' => 'integer'],
'post_count' => ['type' => 'integer', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
];
public $records = [
['category_id' => 1, 'user_id' => 1, 'post_count' => 1],
['category_id' => 2, 'user_id' => 1, 'post_count' => 1],
['category_id' => 2, 'user_id' => 2, 'post_count' => 1]
];
}
@@ -50,7 +50,8 @@ class CounterCacheBehaviorTest extends TestCase
public $fixtures = [
'core.counter_cache_categories',
'core.counter_cache_posts',
'core.counter_cache_users'
'core.counter_cache_users',
'core.counter_cache_user_category_posts'
];
/**
@@ -78,6 +79,12 @@ public function setUp()
'table' => 'counter_cache_posts',
'connection' => $this->connection
]);
$this->user_category_posts = new Table([
'alias' => 'UserCategoryPosts',
'table' => 'counter_cache_user_category_posts',
'connection' => $this->connection
]);
}
/**
@@ -365,6 +372,35 @@ public function testMultiple()
$this->assertEquals(2, $before->get('post_count'));
$this->assertEquals(3, $after->get('post_count'));
}
/**
* Tests to see that the binding key configuration is respected.
*
* @return void
*/
public function testBindingKey()
{
$this->post->hasMany('UserCategoryPosts', [
'bindingKey' => ['category_id', 'user_id'],
'foreignKey' => ['category_id', 'user_id']
]);
$this->post->association('UserCategoryPosts')->target($this->user_category_posts);
$this->post->addBehavior('CounterCache', [
'UserCategoryPosts' => ['post_count']
]);
$before = $this->user_category_posts->find()
->where(['user_id' => 1, 'category_id' => 2])
->first();
$entity = $this->_getEntity()->set('category_id', 2);
$this->post->save($entity);
$after = $this->user_category_posts->find()
->where(['user_id' => 1, 'category_id' => 2])
->first();
$this->assertEquals(1, $before->get('post_count'));
$this->assertEquals(2, $after->get('post_count'));
}
/**
* Get a new Entity

0 comments on commit f06c6cf

Please sign in to comment.
You can’t perform that action at this time.