Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More work towards having a query builder callback for contains
  • Loading branch information
lorenzo committed Jan 3, 2014
1 parent 5a9f8c0 commit 808a72d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 40 deletions.
9 changes: 9 additions & 0 deletions Cake/ORM/Association.php
Expand Up @@ -393,6 +393,7 @@ public function attachTo(Query $query, array $options = []) {
'includeFields' => true,
'foreignKey' => $this->foreignKey(),
'conditions' => [],
'fields' => [],
'type' => $this->joinType(),
'table' => $target->table()
];
Expand All @@ -405,6 +406,14 @@ public function attachTo(Query $query, array $options = []) {
}
}

$options['conditions'] = $query->newExpr()->add($options['conditions']);

if (!empty($options['queryBuilder'])) {
$newQuery = $options['queryBuilder']($target->query());
$options['fields'] = $newQuery->clause('select') ?: $options['fields'];
$options['conditions']->add($newQuery->clause('where'));
}

$joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
$query->join([$target->alias() => array_intersect_key($options, $joinOptions)]);

Expand Down
5 changes: 5 additions & 0 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -249,6 +249,11 @@ public function eagerLoader(array $options) {
'strategy' => $this->strategy()
];
$fetchQuery = $this->_buildQuery($options);

if (!empty($options['queryBuilder'])) {
$fetchQuery = $options['queryBuilder']($fetchQuery);
}

$resultMap = [];
$key = $options['foreignKey'];
$property = $this->target()->association($this->junction()->alias())->property();
Expand Down
4 changes: 4 additions & 0 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -205,6 +205,10 @@ protected function _buildQuery($options) {
$fetchQuery->contain($options['contain']);
}

if (!empty($options['queryBuilder'])) {
$options['queryBuilder']($fetchQuery);
}

return $fetchQuery;
}

Expand Down
5 changes: 5 additions & 0 deletions Cake/ORM/Association/HasMany.php
Expand Up @@ -85,6 +85,11 @@ public function eagerLoader(array $options) {
'strategy' => $this->strategy()
];
$fetchQuery = $this->_buildQuery($options);

if (!empty($options['queryBuilder'])) {
$fetchQuery = $options['queryBuilder']($fetchQuery);
}

$resultMap = [];
$key = $options['foreignKey'];
foreach ($fetchQuery->all() as $result) {
Expand Down
17 changes: 9 additions & 8 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -221,9 +222,9 @@ public function testAttachTo() {
$association = new BelongsToMany('Tags', $config);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
],
]),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -234,10 +235,10 @@ public function testAttachTo() {

$query->expects($this->at(2))->method('join')->with([
'ArticlesTags' => [
'conditions' => [
'conditions' => new QueryExpression([
['Articles.id' => $field1],
['Tags.id' => $field2]
],
]),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down Expand Up @@ -275,9 +276,9 @@ public function testAttachToNoFields() {
$association = new BelongsToMany('Tags', $config);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
],
]),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -288,10 +289,10 @@ public function testAttachToNoFields() {

$query->expects($this->at(1))->method('join')->with([
'ArticlesTags' => [
'conditions' => [
'conditions' => new QueryExpression([
['Articles.id' => $field1],
['Tags.id' => $field2]
],
]),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down
13 changes: 7 additions & 6 deletions Cake/Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -95,10 +96,10 @@ public function testAttachTo() {
$field = new IdentifierExpression('Clients.company_id');
$query->expects($this->once())->method('join')->with([
'Companies' => [
'conditions' => [
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field]
],
]),
'table' => 'companies',
'type' => 'LEFT'
]
Expand All @@ -125,9 +126,9 @@ public function testAttachToConfigOverride() {
$association = new BelongsTo('Companies', $config);
$query->expects($this->once())->method('join')->with([
'Companies' => [
'conditions' => [
'conditions' => new QueryExpression([
'Companies.is_active' => false
],
]),
'type' => 'LEFT',
'table' => 'companies',
]
Expand Down Expand Up @@ -160,10 +161,10 @@ public function testAttachToNoFields() {
$field = new IdentifierExpression('Clients.company_id');
$query->expects($this->once())->method('join')->with([
'Companies' => [
'conditions' => [
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field]
],
]),
'type' => 'LEFT',
'table' => 'companies',
]
Expand Down
13 changes: 7 additions & 6 deletions Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -378,10 +379,10 @@ public function testAttachTo() {
$association = new HasMany('Articles', $config);
$query->expects($this->once())->method('join')->with([
'Articles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Articles.is_active' => true,
['Authors.id' => $field]
],
]),
'type' => 'INNER',
'table' => 'articles'
]
Expand Down Expand Up @@ -409,9 +410,9 @@ public function testAttachToConfigOverride() {
$association = new HasMany('Articles', $config);
$query->expects($this->once())->method('join')->with([
'Articles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Articles.is_active' => false
],
]),
'type' => 'INNER',
'table' => 'articles'
]
Expand Down Expand Up @@ -444,10 +445,10 @@ public function testAttachToNoFields() {
$association = new HasMany('Articles', $config);
$query->expects($this->once())->method('join')->with([
'Articles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Articles.is_active' => true,
['Authors.id' => $field]
],
]),
'type' => 'INNER',
'table' => 'articles'
]
Expand Down
13 changes: 7 additions & 6 deletions Cake/Test/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Association\HasOne;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -95,10 +96,10 @@ public function testAttachTo() {
$field = new IdentifierExpression('Profiles.user_id');
$query->expects($this->once())->method('join')->with([
'Profiles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Profiles.is_active' => true,
['Users.id' => $field],
],
]),
'type' => 'INNER',
'table' => 'profiles'
]
Expand Down Expand Up @@ -127,9 +128,9 @@ public function testAttachToConfigOverride() {
$association = new HasOne('Profiles', $config);
$query->expects($this->once())->method('join')->with([
'Profiles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Profiles.is_active' => false
],
]),
'type' => 'INNER',
'table' => 'profiles'
]
Expand Down Expand Up @@ -162,10 +163,10 @@ public function testAttachToNoFields() {
$field = new IdentifierExpression('Profiles.user_id');
$query->expects($this->once())->method('join')->with([
'Profiles' => [
'conditions' => [
'conditions' => new QueryExpression([
'Profiles.is_active' => true,
['Users.id' => $field],
],
]),
'type' => 'INNER',
'table' => 'profiles'
]
Expand Down
28 changes: 14 additions & 14 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -125,69 +125,69 @@ public function testContainToJoinsOneLevel() {
->with(['clients' => [
'table' => 'clients',
'type' => 'LEFT',
'conditions' => [
'conditions' => new QueryExpression([
['clients.id' => new IdentifierExpression('foo.client_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('join')
->with(['orders' => [
'table' => 'orders',
'type' => 'INNER',
'conditions' => [
'conditions' => new QueryExpression([
['clients.id' => new IdentifierExpression('orders.client_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(2))->method('join')
->with(['orderTypes' => [
'table' => 'order_types',
'type' => 'LEFT',
'conditions' => [
'conditions' => new QueryExpression([
['orderTypes.id' => new IdentifierExpression('orders.order_type_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(3))->method('join')
->with(['stuff' => [
'table' => 'things',
'type' => 'INNER',
'conditions' => [
'conditions' => new QueryExpression([
['orders.id' => new IdentifierExpression('stuff.order_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(4))->method('join')
->with(['stuffTypes' => [
'table' => 'stuff_types',
'type' => 'LEFT',
'conditions' => [
'conditions' => new QueryExpression([
['stuffTypes.id' => new IdentifierExpression('stuff.stuff_type_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(5))->method('join')
->with(['companies' => [
'table' => 'organizations',
'type' => 'LEFT',
'conditions' => [
'conditions' => new QueryExpression([
['companies.id' => new IdentifierExpression('clients.organization_id')]
]
])
]])
->will($this->returnValue($query));

$query->expects($this->at(6))->method('join')
->with(['categories' => [
'table' => 'categories',
'type' => 'LEFT',
'conditions' => [
'conditions' => new QueryExpression([
['categories.id' => new IdentifierExpression('companies.category_id')]
]
])
]])
->will($this->returnValue($query));

Expand Down

0 comments on commit 808a72d

Please sign in to comment.