Skip to content

Commit

Permalink
Making more progress towards supporting limit and offset with subquer…
Browse files Browse the repository at this point in the history
…y strategy
  • Loading branch information
lorenzo committed Sep 11, 2014
1 parent ce31b72 commit 571fc63
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 109 deletions.
18 changes: 8 additions & 10 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -31,7 +31,7 @@ class BelongsToMany extends Association {

use ExternalAssociationTrait {
_options as _externalOptions;
_addFilteringCondition as _addExternalConditions;
_buildQuery as _buildBaseQuery;
}

/**
Expand Down Expand Up @@ -863,19 +863,17 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
}

/**
* Appends any conditions required to load the relevant set of records in the
* target table query given a filter key and some filtering values.
* Auxiliary function to construct a new Query object to return all the records
* in the target table that are associated to those specified in $options from
* the source table
*
* @param \Cake\ORM\Query $query target table's query
* @param string $key the fields that should be used for filtering
* @param mixed $filter the value that should be used to match for $key
* @param array $options options accepted by eagerLoader()
* @return \Cake\ORM\Query
* @throws \InvalidArgumentException When a key is required for associations but not selected.
*/
protected function _addFilteringCondition($query, $key, $filter) {
protected function _buildQuery($options) {
$name = $this->_junctionAssociationName();
return $query->matching($name, function($q) use ($key, $filter) {
return $this->_addExternalConditions($q, $key, $filter);
});
return $this->_buildBaseQuery($options)->matching($name);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -125,7 +125,7 @@ protected function _buildQuery($options) {

public function _addFilteringJoin($query, $key, $subquery) {
$filter = [];
$aliasedTable = uniqid();
$aliasedTable = uniqid('c');

foreach ($subquery->clause('select') as $aliasedField => $field) {
$filter[] = new IdentifierExpression("$aliasedTable.$aliasedField");
Expand Down
98 changes: 0 additions & 98 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -219,104 +219,6 @@ public function testSaveStrategyInvalid() {
$assoc = new BelongsToMany('Test', ['saveStrategy' => 'depsert']);
}

/**
* 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']);

$table = TableRegistry::get('ArticlesTags');
$table->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'] = [
[
'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'] = [
[
'id' => 2,
'name' => 'bar',
'site_id' => 2,
'_joinData' => ['article_id' => 2, 'site_id' => 2]
]
];
$this->assertEquals($row, $result);
}

/**
* Test cascading deletes.
*
Expand Down

0 comments on commit 571fc63

Please sign in to comment.