浏览代码

Adding a fix for #5463

Jose Lorenzo Rodriguez 11 年之前
父节点
当前提交
f111af39ad
共有 2 个文件被更改,包括 27 次插入3 次删除
  1. 8 3
      src/ORM/ResultSet.php
  2. 19 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 8 - 3
src/ORM/ResultSet.php

@@ -283,11 +283,12 @@ class ResultSet implements ResultSetInterface {
 		$map = $this->_query->eagerLoader()->associationsMap($this->_defaultTable);
 		$this->_matchingMap = (new Collection($map))
 			->match(['matching' => true])
-			->compile();
+			->toArray();
 
 		$this->_containMap = (new Collection(array_reverse($map)))
 			->match(['matching' => false])
-			->compile();
+			->indexBy('nestKey')
+			->toArray();
 	}
 
 /**
@@ -331,7 +332,10 @@ class ResultSet implements ResultSetInterface {
 				}
 				list($table, $field) = explode('__', $key);
 				$results['_matchingData'][$table][$field] = $value;
-				unset($row[$key]);
+
+				if (!isset($this->_containMap[$table])) {
+					unset($row[$key]);
+				}
 			}
 			if (empty($results['_matchingData'][$matching['alias']])) {
 				continue;
@@ -343,6 +347,7 @@ class ResultSet implements ResultSetInterface {
 			);
 
 			if ($this->_hydrate) {
+				$options['source'] = $matching['alias'];
 				$entity = new $matching['entityClass']($results['_matchingData'][$matching['alias']], $options);
 				$entity->clean();
 				$results['_matchingData'][$matching['alias']] = $entity;

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

@@ -576,4 +576,23 @@ class QueryRegressionTest extends TestCase {
 		$this->assertCount(3, $results->toArray());
 	}
 
+/**
+ * Checks that matching and contain can be called for the same belongsTo association
+ *
+ * @see https://github.com/cakephp/cakephp/issues/5463
+ * @return void
+ */
+	public function testFindMatchingAndContain() {
+		$table = TableRegistry::get('Articles');
+		$table->belongsTo('Authors');
+		$article = $table->find()
+			->contain('Authors')
+			->matching('Authors', function ($q) {
+				return $q->where(['Authors.id' => 1]);
+			})
+			->first();
+		$this->assertNotNull($article->author);
+		$this->assertEquals($article->author, $article->_matchingData['Authors']);
+	}
+
 }