Browse Source

Fixing error were results from joined tables would dissapear from
resultset

Jose Lorenzo Rodriguez 12 years ago
parent
commit
b4a490611c
2 changed files with 33 additions and 1 deletions
  1. 8 1
      src/ORM/ResultSet.php
  2. 25 0
      tests/TestCase/ORM/QueryTest.php

+ 8 - 1
src/ORM/ResultSet.php

@@ -347,7 +347,7 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
  */
 	protected function _groupResult($row) {
 		$defaultAlias = $this->_defaultTable->alias();
-		$results = [];
+		$results = $presentAliases = [];
 		foreach ($row as $key => $value) {
 			$table = $defaultAlias;
 			$field = $key;
@@ -363,9 +363,11 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 				list($table, $field) = $this->_map[$key];
 			}
 
+			$presentAliases[$table] = true;
 			$results[$table][$field] = $value;
 		}
 
+		unset($presentAliases[$defaultAlias]);
 		$results[$defaultAlias] = $this->_castValues(
 			$this->_defaultTable,
 			$results[$defaultAlias]
@@ -384,6 +386,7 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 			$instance = $assoc['instance'];
 			$target = $instance->target();
 			$results[$alias] = $this->_castValues($target, $results[$alias]);
+			unset($presentAliases[$alias]);
 			$options['source'] = $target->alias();
 
 			if ($this->_hydrate && $assoc['canBeJoined']) {
@@ -394,6 +397,10 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 			$results = $instance->transformRow($results);
 		}
 
+		foreach($presentAliases as $alias => $present) {
+			$results[$defaultAlias][$alias] = $results[$alias];
+		}
+
 		$options['source'] = $defaultAlias;
 		$results = $results[$defaultAlias];
 		if ($this->_hydrate && !($results instanceof Entity)) {

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

@@ -1841,4 +1841,29 @@ class QueryTest extends TestCase {
 		$query->all();
 	}
 
+/**
+ * Tests that columns from manual joins are also contained in the result set
+ *
+ * @return void
+ */
+	public function testColumnsFromJoin() {
+		$table = TableRegistry::get('articles');
+		$results = $table->find()
+			->select(['title', 'person.name'])
+			->join([
+				'person' => [
+					'table' => 'authors',
+					'conditions' => ['person.id = articles.author_id']
+				]
+			])
+			->hydrate(false)
+			->toArray();
+		$expected = [
+			['title' => 'First Article', 'person' => ['name' => 'mariano']],
+			['title' => 'Second Article', 'person' => ['name' => 'larry']],
+			['title' => 'Third Article', 'person' => ['name' => 'mariano']],
+		];
+		$this->assertSame($expected, $results);
+	}
+
 }