Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@markstory markstory authored
View
18 lib/Cake/Model/Behavior/ContainableBehavior.php
@@ -91,21 +91,25 @@ public function setup(Model $Model, $settings = array()) {
*/
public function beforeFind(Model $Model, $query) {
$reset = (isset($query['reset']) ? $query['reset'] : true);
- $noContain = (
- (isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) ||
- (isset($query['contain']) && empty($query['contain']))
- );
+ $noContain = false;
$contain = array();
+
if (isset($this->runtime[$Model->alias]['contain'])) {
+ $noContain = empty($this->runtime[$Model->alias]['contain']);
$contain = $this->runtime[$Model->alias]['contain'];
unset($this->runtime[$Model->alias]['contain']);
}
+
if (isset($query['contain'])) {
- $contain = array_merge($contain, (array)$query['contain']);
+ $noContain = $noContain || empty($query['contain']);
+ if ($query['contain'] !== false) {
+ $contain = array_merge($contain, (array)$query['contain']);
+ }
}
+ $noContain = $noContain && empty($contain);
+
if (
- $noContain || !$contain || in_array($contain, array(null, false), true) ||
- (isset($contain[0]) && $contain[0] === null)
+ $noContain || empty($contain) || (isset($contain[0]) && $contain[0] === null)
) {
if ($noContain) {
$query['recursive'] = -1;
View
13 lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php
@@ -262,6 +262,19 @@ public function testContain() {
}
/**
+ * Test that mixing contain() and the contain find option.
+ *
+ * @return void
+ */
+ public function testContainAndContainOption() {
+ $this->Article->contain();
+ $r = $this->Article->find('all', array(
+ 'contain' => array('Comment')
+ ));
+ $this->assertTrue(isset($r[0]['Comment']), 'No comment returned');
+ }
+
+/**
* testFindEmbeddedNoBindings method
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.