Jose Lorenzo Rodriguez 11 years ago
parent
commit
b33b703ac9
2 changed files with 24 additions and 0 deletions
  1. 3 0
      src/ORM/ResultSet.php
  2. 21 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 3 - 0
src/ORM/ResultSet.php

@@ -383,6 +383,9 @@ class ResultSet implements ResultSetInterface
         }
 
         foreach ($this->_matchingMap as $alias => $assoc) {
+            if (!isset($map[$alias])) {
+                continue;
+            }
             $this->_matchingMapColumns[$alias] = $map[$alias];
             unset($map[$alias]);
         }

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

@@ -818,4 +818,25 @@ class QueryRegressionTest extends TestCase
 
         $this->assertEquals([3 => 1, 4 => 1, 5 => 1], $results->toArray());
     }
+
+    /**
+     * Tests that using matching and selecting no fields for that association
+     * will no trigger any errors and fetch the right results
+     *
+     * @see https://github.com/cakephp/cakephp/issues/6223
+     * @return void
+     */
+    public function testMatchingWithNoFields()
+    {
+        $table = TableRegistry::get('Users');
+        $table->hasMany('Comments');
+        $results = $table->find()
+            ->select(['Users.id'])
+            ->matching('Comments', function ($q) {
+                return $q->where(['Comments.id' => 1]);
+            })
+            ->extract('id')
+            ->toList();
+        $this->assertEquals([2], $results);
+    }
 }