Skip to content

Commit

Permalink
Fixing SQL errors when trying to eagerload associations from empty
Browse files Browse the repository at this point in the history
results, fixes #3167 and #3113
  • Loading branch information
lorenzo committed Mar 31, 2014
1 parent 0486821 commit 8b202d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ORM/EagerLoader.php
Expand Up @@ -367,6 +367,12 @@ public function loadExternal($query, $statement) {
foreach ($external as $meta) {
$contain = $meta['associations'];
$alias = $meta['instance']->source()->alias();

$isSelect = $meta['instance']->strategy() === $meta['instance']::STRATEGY_SELECT;
if ($isSelect && empty($collected[$alias])) {
continue;
}

$keys = isset($collected[$alias]) ? $collected[$alias] : null;
$f = $meta['instance']->eagerLoader(
$meta['config'] + ['query' => $query, 'contain' => $contain, 'keys' => $keys]
Expand Down
15 changes: 14 additions & 1 deletion tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -30,7 +30,7 @@ class QueryRegressionTest extends TestCase {
*
* @var array
*/
public $fixtures = ['core.user'];
public $fixtures = ['core.user', 'core.article', 'core.tag', 'core.articles_tag'];

/**
* Tear down
Expand All @@ -53,4 +53,17 @@ public function testSelectTimestampColumn() {
$this->assertEquals(new \DateTime('2007-03-17 01:18:31'), $user->updated);
}

/**
* Tests that EagerLoader does not try to create queries for associations having no
* keys to compare against
*
* @return void
*/
public function testEagerLoadingFromEmptyResults() {
$table = TableRegistry::get('Articles');
$table->belongsToMany('ArticlesTags');
$results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
$this->assertEmpty($results);
}

}

0 comments on commit 8b202d9

Please sign in to comment.