Browse Source

Merge branch 'issue-7609' into master

Use bindingKey to allow for multi column conditions on counter caches.

Refs #7618
Refs #7608
Mark Story 10 years ago
parent
commit
485475c2e7

+ 2 - 2
src/ORM/Behavior/CounterCacheBehavior.php

@@ -135,11 +135,11 @@ class CounterCacheBehavior extends Behavior
     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);
         }

+ 39 - 0
tests/Fixture/CounterCacheUserCategoryPostsFixture.php

@@ -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]
+    ];
+}

+ 37 - 1
tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php

@@ -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 @@ class CounterCacheBehaviorTest extends TestCase
             '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 @@ class CounterCacheBehaviorTest extends TestCase
         $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