Browse Source

Merge pull request #5315 from cakephp/issue-5282

Raise an exception on invalid belongs to many data.
José Lorenzo Rodríguez 11 years ago
parent
commit
dbbbff333c
2 changed files with 21 additions and 0 deletions
  1. 7 0
      src/ORM/Association/BelongsToMany.php
  2. 14 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 7 - 0
src/ORM/Association/BelongsToMany.php

@@ -277,6 +277,7 @@ class BelongsToMany extends Association {
  * @param \Cake\ORM\Query $fetchQuery The query to get results from
  * @param array $options The options passed to the eager loader
  * @return array
+ * @throws \RuntimeException when the association property is not part of the results set.
  */
 	protected function _buildResultMap($fetchQuery, $options) {
 		$resultMap = [];
@@ -285,6 +286,12 @@ class BelongsToMany extends Association {
 		$hydrated = $fetchQuery->hydrate();
 
 		foreach ($fetchQuery->all() as $result) {
+			if (!isset($result[$property])) {
+				throw new \RuntimeException(sprintf(
+					'"%s" is missing from the belongsToMany results. Results cannot be created.',
+					$property
+				));
+			}
 			$result[$this->_junctionProperty] = $result[$property];
 			unset($result[$property]);
 

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

@@ -80,6 +80,20 @@ class QueryRegressionTest extends TestCase {
 	}
 
 /**
+ * Tests that eagerloading belongsToMany with find list fails with a helpful message.
+ *
+ * @expectedException \RuntimeException
+ * @return void
+ */
+	public function testEagerLoadingBelongsToManyList() {
+		$table = TableRegistry::get('Articles');
+		$table->belongsToMany('Tags', [
+			'finder' => 'list'
+		]);
+		$table->find()->contain('Tags')->toArray();
+	}
+
+/**
  * Tests that duplicate aliases in contain() can be used, even when they would
  * naturally be attached to the query instead of eagerly loaded. What should
  * happen here is that One of the duplicates will be changed to be loaded using