Skip to content
Permalink
Browse files

Making sure a variable is array before checking values on it, fixes

  • Loading branch information...
lorenzo committed Jan 6, 2014
1 parent e38dead commit 6b058706214231630680de6a3406fe0a41e1e3ca
Showing with 27 additions and 1 deletion.
  1. +2 −1 Cake/ORM/Query.php
  2. +25 −0 Test/TestCase/ORM/QueryTest.php
@@ -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;
}
@@ -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.
You can’t perform that action at this time.