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
This commit is contained in:
mark_story 2012-10-20 14:51:52 -04:00
parent ea467e72d7
commit 888b1f4795
2 changed files with 24 additions and 7 deletions

View file

@ -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;

View file

@ -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
*