From 888b1f47959e386021d4afa4491cbef5dab41721 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 20 Oct 2012 14:51:52 -0400 Subject: [PATCH] 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 --- .../Model/Behavior/ContainableBehavior.php | 18 +++++++++++------- .../Model/Behavior/ContainableBehaviorTest.php | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Model/Behavior/ContainableBehavior.php b/lib/Cake/Model/Behavior/ContainableBehavior.php index 98c067468..799f6e238 100644 --- a/lib/Cake/Model/Behavior/ContainableBehavior.php +++ b/lib/Cake/Model/Behavior/ContainableBehavior.php @@ -91,21 +91,25 @@ class ContainableBehavior extends ModelBehavior { */ 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; diff --git a/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php index 8345c87d6..e43f49bfa 100644 --- a/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/ContainableBehaviorTest.php @@ -261,6 +261,19 @@ class ContainableBehaviorTest extends CakeTestCase { $this->assertFalse(Set::matches('/Comment/User', $r)); } +/** + * 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 *