Skip to content

Commit

Permalink
Added fix for the subquery strategy using the wrong alias when compar…
Browse files Browse the repository at this point in the history
…ing.

Previously, the subquery strategy was naively assuming that the alias used for
comparing could be derived from the original query, but the right thing to do was
to use the alias from the association object itself.

Fixes #5769
  • Loading branch information
lorenzo committed Jan 29, 2015
1 parent e236933 commit 1097eef
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -141,7 +141,7 @@ protected function _buildQuery($options)
public function _addFilteringJoin($query, $key, $subquery)
{
$filter = [];
$aliasedTable = $subquery->repository()->alias();
$aliasedTable = $this->source()->alias();

foreach ($subquery->clause('select') as $aliasedField => $field) {
$filter[] = new IdentifierExpression($field);
Expand Down Expand Up @@ -239,7 +239,7 @@ protected function _buildSubquery($query)
$keys = (array)$this->foreignKey();
}

$fields = $query->aliasFields($keys);
$fields = $query->aliasFields($keys, $this->source()->alias());
return $filterQuery->select($fields, true);
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Fixture/AuthorsTagsFixture.php
@@ -0,0 +1,49 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* AuthorsTags fixture
*
*/
class AuthorsTagsFixture extends TestFixture
{

/**
* fields property
*
* @var array
*/
public $fields = [
'author_id' => ['type' => 'integer', 'null' => false],
'tag_id' => ['type' => 'integer', 'null' => false],
'_constraints' => [
'unique_tag' => ['type' => 'primary', 'columns' => ['author_id', 'tag_id']],
]
];

/**
* records property
*
* @var array
*/
public $records = [
['author_id' => 3, 'tag_id' => 1],
['author_id' => 3, 'tag_id' => 2],
['author_id' => 2, 'tag_id' => 1],
['author_id' => 2, 'tag_id' => 3]
];
}
34 changes: 32 additions & 2 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -42,6 +42,7 @@ class QueryRegressionTest extends TestCase
'core.authors',
'core.special_tags',
'core.translates',
'core.authors_tags',
];

/**
Expand Down Expand Up @@ -466,7 +467,7 @@ public function testAssociationSubQueryNoOffset()
}

/**
* Tests that using the subquery strategy in a deep assotiatin returns the right results
* Tests that using the subquery strategy in a deep assotiation returns the right results
*
* @see https://github.com/cakephp/cakephp/issues/4484
* @return void
Expand All @@ -478,14 +479,43 @@ public function testDeepBelongsToManySubqueryStrategy()
$table->Articles->belongsToMany('Tags', [
'strategy' => 'subquery'
]);
$table->Articles->Tags->junction();

$result = $table->find()->contain(['Articles.Tags'])->toArray();
$this->assertEquals(
['tag1', 'tag3'],
collection($result[2]->articles[0]->tags)->extract('name')->toArray()
);
}

/**
* Tests that using the subquery strategy in a deep assotiation returns the right results
*
* @see https://github.com/cakephp/cakephp/issues/5769
* @return void
*/
public function testDeepBelongsToManySubqueryStrategy2()
{
$table = TableRegistry::get('Authors');
$table->hasMany('Articles');
$table->Articles->belongsToMany('Tags', [
'strategy' => 'subquery'
]);
$table->belongsToMany('Tags', [
'strategy' => 'subquery',
]);
$table->Articles->belongsTo('Authors');

$result = $table->Articles->find()
->where(['Authors.id > 1'])
->contain(['Authors.Tags'])
->toArray();
$this->assertEquals(
['tag1', 'tag2'],
collection($result[0]->author->tags)->extract('name')->toArray()
);
$this->assertEquals(3, $result[0]->author->id);
}

/**
* Tests that getting the count of a query having containments return
* the correct results
Expand Down

0 comments on commit 1097eef

Please sign in to comment.