Skip to content
This repository
Browse code

Fix issue with using contain() and query[contain]

When contain() and query['contain'] = array(...) were used together
the query['contain'] values where not respected.

Fixes #3287
  • Loading branch information...
commit 888b1f47959e386021d4afa4491cbef5dab41721 1 parent ea467e7
Mark Story authored October 20, 2012
18  lib/Cake/Model/Behavior/ContainableBehavior.php
@@ -91,21 +91,25 @@ public function setup(Model $Model, $settings = array()) {
91 91
  */
92 92
 	public function beforeFind(Model $Model, $query) {
93 93
 		$reset = (isset($query['reset']) ? $query['reset'] : true);
94  
-		$noContain = (
95  
-			(isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) ||
96  
-			(isset($query['contain']) && empty($query['contain']))
97  
-		);
  94
+		$noContain = false;
98 95
 		$contain = array();
  96
+
99 97
 		if (isset($this->runtime[$Model->alias]['contain'])) {
  98
+			$noContain = empty($this->runtime[$Model->alias]['contain']);
100 99
 			$contain = $this->runtime[$Model->alias]['contain'];
101 100
 			unset($this->runtime[$Model->alias]['contain']);
102 101
 		}
  102
+
103 103
 		if (isset($query['contain'])) {
104  
-			$contain = array_merge($contain, (array)$query['contain']);
  104
+			$noContain = $noContain || empty($query['contain']);
  105
+			if ($query['contain'] !== false) {
  106
+				$contain = array_merge($contain, (array)$query['contain']);
  107
+			}
105 108
 		}
  109
+		$noContain = $noContain && empty($contain);
  110
+
106 111
 		if (
107  
-			$noContain || !$contain || in_array($contain, array(null, false), true) ||
108  
-			(isset($contain[0]) && $contain[0] === null)
  112
+			$noContain || empty($contain) || (isset($contain[0]) && $contain[0] === null)
109 113
 		) {
110 114
 			if ($noContain) {
111 115
 				$query['recursive'] = -1;
13  lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php
@@ -262,6 +262,19 @@ public function testContain() {
262 262
 	}
263 263
 
264 264
 /**
  265
+ * Test that mixing contain() and the contain find option.
  266
+ *
  267
+ * @return void
  268
+ */
  269
+	public function testContainAndContainOption() {
  270
+		$this->Article->contain();
  271
+		$r = $this->Article->find('all', array(
  272
+			'contain' => array('Comment')
  273
+		));
  274
+		$this->assertTrue(isset($r[0]['Comment']), 'No comment returned');
  275
+	}
  276
+
  277
+/**
265 278
  * testFindEmbeddedNoBindings method
266 279
  *
267 280
  * @return void

0 notes on commit 888b1f4

Please sign in to comment.
Something went wrong with that request. Please try again.