Browse Source

Merge pull request #6339 from cakephp/issue-6326

Don't assume that queries always contain fields
José Lorenzo Rodríguez 11 years ago
parent
commit
3a3de18ca3
2 changed files with 23 additions and 1 deletions
  1. 3 1
      src/ORM/ResultSet.php
  2. 20 0
      tests/TestCase/ORM/QueryTest.php

+ 3 - 1
src/ORM/ResultSet.php

@@ -586,7 +586,9 @@ class ResultSet implements ResultSetInterface
         }
 
         $options['source'] = $this->_defaultTable->registryAlias();
-        $results = $results[$defaultAlias];
+        if (isset($results[$defaultAlias])) {
+            $results = $results[$defaultAlias];
+        }
         if ($this->_hydrate && !($results instanceof Entity)) {
             $results = new $this->_entityClass($results, $options);
         }

+ 20 - 0
tests/TestCase/ORM/QueryTest.php

@@ -2041,6 +2041,26 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test finding fields on the non-default table that
+     * have the same name as the primary table.
+     *
+     * @return void
+     */
+    public function testContainSelectedFields()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->belongsTo('Authors');
+
+        $query = $table->find()
+            ->contain(['Authors'])
+            ->order(['Authors.id' => 'asc'])
+            ->select(['Authors.id']);
+        $results = $query->extract('Authors.id')->toList();
+        $expected = [1, 1, 3];
+        $this->assertEquals($expected, $results);
+    }
+
+    /**
      * Tests that it is possible to attach more association when using a query
      * builder for other associations
      *