Skip to content

Commit

Permalink
Making sure a variable is array before checking values on it, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 6, 2014
1 parent e38dead commit 6b05870
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cake/ORM/Query.php
Expand Up @@ -293,7 +293,8 @@ public function contain($associations = null, $override = false) {
}

$associations = (array)$associations;
if (isset(current($associations)['instance'])) {
$current = current($associations);
if (is_array($current) && isset($current['instance'])) {
$this->_containments = $this->_normalizedContainments = $associations;
return $this;
}
Expand Down
25 changes: 25 additions & 0 deletions Test/TestCase/ORM/QueryTest.php
Expand Up @@ -1703,4 +1703,29 @@ public function testCacheWriteIntegration() {
$query->all();
}


/**
* Integration test to show filtering associations using contain and a closure
*
* @return void
*/
public function testContainWithClosure() {
$table = TableRegistry::get('authors');
$table->hasMany('articles');
$query = new Query($this->connection, $table);
$query
->select()
->contain(['articles' => function($q) {
return $q->where(['articles.id' => 1]);
}]);

$ids = [];
foreach ($query as $entity) {
foreach ((array)$entity->articles as $article) {
$ids[] = $article->id;
}
}
$this->assertEquals([1], array_unique($ids));
}

}

0 comments on commit 6b05870

Please sign in to comment.