Browse Source

Fix `notMatching()` without query builder callback triggers an error.

ndm2 9 years ago
parent
commit
20067e3d3f
2 changed files with 24 additions and 1 deletions
  1. 3 1
      src/ORM/Association/BelongsToMany.php
  2. 21 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 3 - 1
src/ORM/Association/BelongsToMany.php

@@ -387,7 +387,9 @@ class BelongsToMany extends Association
             ->where($options['conditions'])
             ->andWhere($this->junctionConditions());
 
-        $subquery = $options['queryBuilder']($subquery);
+        if (!empty($options['queryBuilder'])) {
+            $subquery = $options['queryBuilder']($subquery);
+        }
 
         $assoc = $junction->association($this->target()->alias());
         $conditions = $assoc->_joinCondition([

+ 21 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -1394,4 +1394,25 @@ class QueryRegressionTest extends TestCase
         $result = $query->where(['updated >' => $query->func()->now('datetime')])->first();
         $this->assertSame(6, $result->id);
     }
+
+    /**
+     * Tests that `notMatching()` can be used on `belongsToMany`
+     * associations without passing a query builder callback.
+     *
+     * @return void
+     */
+    public function testNotMatchingForBelongsToManyWithoutQueryBuilder()
+    {
+        $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
+
+        $Articles = TableRegistry::get('Articles');
+        $Articles->belongsToMany('Tags');
+
+        $result = $Articles->find('list')->notMatching('Tags')->toArray();
+        $expected = [
+            3 => 'Third Article'
+        ];
+
+        $this->assertEquals($expected, $result);
+    }
 }