Skip to content

Commit

Permalink
Improving eager loading tet for BelongsToMany when using composite keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 14, 2014
1 parent ff6eb98 commit ac3710d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -273,9 +273,9 @@ protected function _addFilteringCondition($query, $key, $filter) {
$types[] = $defaults[$k]; $types[] = $defaults[$k];
} }
} }
$query->andWhere(new TupleComparison($key, $filter, $types, 'IN')); return $query->andWhere(new TupleComparison($key, $filter, $types, 'IN'));
return $query;
} }

return $query->andWhere([$key . ' IN' => $filter]); return $query->andWhere([$key . ' IN' => $filter]);
} }


Expand Down
100 changes: 100 additions & 0 deletions Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -18,6 +18,7 @@


use Cake\Database\Expression\IdentifierExpression; use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression; use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\TupleComparison;
use Cake\ORM\Association\BelongsToMany; use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity; use Cake\ORM\Entity;
use Cake\ORM\Query; use Cake\ORM\Query;
Expand Down Expand Up @@ -795,6 +796,105 @@ public function testEagerLoaderWithQueryBuilder() {
$association->eagerLoader(compact('keys', 'query', 'queryBuilder')); $association->eagerLoader(compact('keys', 'query', 'queryBuilder'));
} }


/**
* Test the eager loader method with no extra options
*
* @return void
*/
public function testEagerLoaderMultipleKeys() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'foreignKey' => ['article_id', 'site_id'],
'targetForeignKey' => ['tag_id', 'site_id']
];
$this->article->primaryKey(['id', 'site_id']);
$this->tag->primaryKey(['id', 'site_id']);
TableRegistry::get('ArticlesTags', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
'tag_id' => ['type' => 'integer'],
'site_id' => ['type' => 'integer'],
]
]);
$association = new BelongsToMany('Tags', $config);
$keys = [[1, 10], [2, 20], [3, 30], [4, 40]];
$query = $this->getMock('Cake\ORM\Query', ['all', 'matching'], [null, null]);

$this->tag->expects($this->once())
->method('find')
->with('all')
->will($this->returnValue($query));

$results = [
[
'id' => 1,
'name' => 'foo',
'site_id' => 1,
'articles_tags' => [
'article_id' => 1,
'site_id' => 1
]
],
[
'id' => 2,
'name' => 'bar',
'site_id' => 2,
'articles_tags' => [
'article_id' => 2,
'site_id' => 2
]
]
];
$query->expects($this->once())
->method('all')
->will($this->returnValue($results));

$tuple = new TupleComparison(
['ArticlesTags.article_id', 'ArticlesTags.site_id'], $keys, [], 'IN'
);
$query->expects($this->once())->method('matching')
->will($this->returnCallback(function($alias, $callable) use ($query, $tuple) {
$this->assertEquals('ArticlesTags', $alias);
$q = $this->getMock('Cake\ORM\Query', [], [null, null]);

$q->expects($this->once())->method('andWhere')
->with($tuple)
->will($this->returnSelf());

$this->assertSame($q, $callable($q));
return $query;
}));

$query->hydrate(false);

$callable = $association->eagerLoader(compact('keys', 'query'));
$row = ['Articles__id' => 1, 'title' => 'article 1', 'Articles__site_id' => 1];
$result = $callable($row);
$row['Tags__Tags'] = [
[
'id' => 1,
'name' => 'foo',
'site_id' => 1,
'_joinData' => ['article_id' => 1, 'site_id' => 1]
]
];
$this->assertEquals($row, $result);

$row = ['Articles__id' => 2, 'title' => 'article 2', 'Articles__site_id' => 2];
$result = $callable($row);
$row['Tags__Tags'] = [
[
'id' => 2,
'name' => 'bar',
'site_id' => 2,
'_joinData' => ['article_id' => 2, 'site_id' => 2]
]
];
$this->assertEquals($row, $result);
}

/** /**
* Test cascading deletes. * Test cascading deletes.
* *
Expand Down
9 changes: 2 additions & 7 deletions Test/TestCase/ORM/CompositeKeysTest.php
Expand Up @@ -15,11 +15,7 @@
namespace Cake\Test\TestCase\ORM; namespace Cake\Test\TestCase\ORM;


use Cake\Database\ConnectionManager; use Cake\Database\ConnectionManager;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\OrderByExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query; use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -133,9 +129,8 @@ public function testHasManyEager($strategy) {
} }


/** /**
* Tests that BelongsToMany associations are correctly eager loaded. * Tests that BelongsToMany associations are correctly eager loaded when multiple
* Also that the query object passes the correct parent model keys to the * foreignKeys are used
* association objects in order to perform eager loading with select strategy
* *
* @dataProvider strategiesProvider * @dataProvider strategiesProvider
* @return void * @return void
Expand Down

0 comments on commit ac3710d

Please sign in to comment.