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

Conflicts:
	cake/libs/model/behaviors/containable.php
  • Loading branch information...
1 parent acd25c3 commit 1904b7a0a6df9e938e6228a7aad8d1e33d044cbc @markstory markstory committed Oct 20, 2012
@@ -98,16 +98,26 @@ function setup(&$Model, $settings = array()) {
*/
function beforeFind(&$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']);
+ }
}
- if ($noContain || !$contain || in_array($contain, array(null, false), true) || (isset($contain[0]) && $contain[0] === null)) {
+ $noContain = $noContain && empty($contain);
+
+ if (
+ $noContain || empty($contain) || (isset($contain[0]) && $contain[0] === null)
+ ) {
if ($noContain) {
$query['recursive'] = -1;
}
@@ -245,6 +245,20 @@ function testContain() {
}
/**
+ * Test that mixing contain() and the contain find option.
+ *
+ * @access public
+ * @return void
+ */
+ 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
*
* @access public

0 comments on commit 1904b7a

Please sign in to comment.