Permalink
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...
1 parent ea467e7 commit 888b1f47959e386021d4afa4491cbef5dab41721 @markstory markstory committed Oct 20, 2012
@@ -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;
@@ -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

0 comments on commit 888b1f4

Please sign in to comment.