Skip to content

Commit

Permalink
Change ValueBinder reset + add unit test
Browse files Browse the repository at this point in the history
I've changed the ValueBinder reset from using ->resetCount() to setting a completely new ValueBinder, as the resetCount was only respected when the select sub function was on another query than the master query.

I've also added unit tests, that fails before adding the fix and works now.
  • Loading branch information
Jesper Skytte Hansen committed May 10, 2016
1 parent 358e6b3 commit 402218b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\TupleComparison;
use Cake\Database\ValueBinder;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -222,7 +223,7 @@ protected function _buildSubquery($query)
$filterQuery->mapReduce(null, null, true);
$filterQuery->formatResults(null, true);
$filterQuery->contain([], true);
$filterQuery->valueBinder()->resetCount();
$filterQuery->valueBinder(new ValueBinder());

if (!$filterQuery->clause('limit')) {
$filterQuery->limit(null);
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Database\IdentifierQuoter;
use Cake\Database\TypeMap;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Association;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
Expand Down Expand Up @@ -535,6 +536,35 @@ public function testPropertyNoPlugin()
$this->assertEquals('addresses', $association->property());
}

/**
* Test that the ValueBinder is reset when using strategy = Association::STRATEGY_SUBQUERY
*
* @return void
*/
public function testValueBinderUpdateOnSubQueryStrategy()
{
$Authors = TableRegistry::get('Authors');
$Authors->hasMany('Articles', [
'strategy' => Association::STRATEGY_SUBQUERY
]);

$query = $Authors->find();
$authorsAndArticles = $query
->select([
'id',
'slug' => $query->func()->concat([
'id' => 'identifier',
'-',
'name' => 'identifier'
])
])
->contain('Articles')
->where(['name' => 'mariano'])
->first();

$this->assertCount(2, $authorsAndArticles->get('articles'));
}

/**
* Assertion method for order by clause contents.
*
Expand Down

0 comments on commit 402218b

Please sign in to comment.