Skip to content

Commit

Permalink
Adding Association::find() to proxy it to the target table
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 18, 2013
1 parent 411093e commit 3acadff
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
16 changes: 16 additions & 0 deletions Cake/ORM/Association.php
Expand Up @@ -430,6 +430,22 @@ public function type() {
return self::ONE_TO_ONE;
}

/**
* Proxies the finding operation to the target table's find method
* and modifies the query accordingly based of this association
* configuration
*
* @param string $type
* @param array $options options for query
* @see \Cake\ORM\Table::find()
* @return \Cake\ORM\Query
*/
public function find($type, $options = []) {
return $this->target()
->find($type, $options)
->where($this->conditions());
}

/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table. If false is returned then
Expand Down
3 changes: 1 addition & 2 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -174,15 +174,14 @@ protected function _resultInjector($fetchQuery, $resultMap) {
protected function _buildQuery($options) {
$target = $this->target();
$alias = $target->alias();
$options['conditions'] = array_merge($this->conditions(), $options['conditions']);
$key = $this->_linkField($options);

$filter = $options['keys'];
if ($options['strategy'] === parent::STRATEGY_SUBQUERY) {
$filter = $this->_buildSubquery($options['query'], $key);
}

$fetchQuery = $target
$fetchQuery = $this
->find('all')
->where($options['conditions'])
->hydrate($options['query']->hydrate());
Expand Down
28 changes: 16 additions & 12 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -398,9 +398,12 @@ public function testEagerLoaderWithDefaults() {
])
->will($this->returnSelf());

$query->expects($this->once())->method('where')
$query->expects($this->at(0))->method('where')
->with(['Tags.name' => 'foo'])
->will($this->returnValue($query));
$query->expects($this->at(1))->method('where')
->with([])
->will($this->returnValue($query));

$query->expects($this->once())->method('order')
->with(['id' => 'ASC'])
Expand Down Expand Up @@ -452,11 +455,12 @@ public function testEagerLoaderWithOverrides() {
])
->will($this->returnSelf());

$query->expects($this->once())->method('where')
->with([
'Tags.name' => 'foo',
'Tags.id !=' => 3
])
$query->expects($this->at(0))->method('where')
->with(['Tags.name' => 'foo'])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('where')
->with(['Tags.id !=' => 3])
->will($this->returnValue($query));

$query->expects($this->once())->method('order')
Expand Down Expand Up @@ -510,7 +514,7 @@ public function testEagerLoaderFieldsException() {
->will($this->returnValue($query));
$query->expects($this->any())->method('contain')->will($this->returnSelf());

$query->expects($this->once())->method('where')->will($this->returnSelf());
$query->expects($this->exactly(2))->method('where')->will($this->returnSelf());

$association->eagerLoader([
'keys' => $keys,
Expand Down Expand Up @@ -560,10 +564,6 @@ public function testEagerLoaderSubquery() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('where')
->with(['Tags.name' => 'foo'])
->will($this->returnSelf());

$expected = clone $parent;
$joins = $expected->join();
unset($joins[1]);
Expand All @@ -572,10 +572,14 @@ public function testEagerLoaderSubquery() {
->select('ArticlesTags.article_id', true)
->join($joins, [], true);

$query->expects($this->once())->method('where')
$query->expects($this->at(0))->method('where')
->with(['Tags.name' => 'foo'])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('where')
->with([])
->will($this->returnValue($query));

$query->expects($this->once())->method('contain')
->with([
'ArticlesTags' => [
Expand Down
19 changes: 15 additions & 4 deletions Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -175,10 +175,14 @@ public function testEagerLoaderWithDefaults() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('where')
$query->expects($this->at(0))->method('where')
->with(['Articles.is_active' => true])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('where')
->with([])
->will($this->returnValue($query));

$query->expects($this->once())->method('andWhere')
->with(['Articles.author_id IN' => $keys])
->will($this->returnValue($query));
Expand Down Expand Up @@ -220,8 +224,12 @@ public function testEagerLoaderWithOverrides() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('where')
->with(['Articles.is_active' => true, 'Articles.id !=' => 3])
$query->expects($this->at(0))->method('where')
->with(['Articles.is_active' => true])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('where')
->with(['Articles.id !=' => 3])
->will($this->returnValue($query));

$query->expects($this->once())->method('andWhere')
Expand Down Expand Up @@ -316,7 +324,10 @@ public function testEagerLoaderSubquery() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('where')
$query->expects($this->at(0))->method('where')
->with([])
->will($this->returnValue($query));
$query->expects($this->at(1))->method('where')
->with([])
->will($this->returnValue($query));

Expand Down

0 comments on commit 3acadff

Please sign in to comment.