diff --git a/Cake/ORM/Association.php b/Cake/ORM/Association.php index fb1697fd2fd..cec09c2968e 100644 --- a/Cake/ORM/Association.php +++ b/Cake/ORM/Association.php @@ -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 diff --git a/Cake/ORM/Association/ExternalAssociationTrait.php b/Cake/ORM/Association/ExternalAssociationTrait.php index e26d4d2cff5..8683d65c3c5 100644 --- a/Cake/ORM/Association/ExternalAssociationTrait.php +++ b/Cake/ORM/Association/ExternalAssociationTrait.php @@ -174,7 +174,6 @@ 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']; @@ -182,7 +181,7 @@ protected function _buildQuery($options) { $filter = $this->_buildSubquery($options['query'], $key); } - $fetchQuery = $target + $fetchQuery = $this ->find('all') ->where($options['conditions']) ->hydrate($options['query']->hydrate()); diff --git a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php index e53c585078a..044c26d8bbe 100644 --- a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php @@ -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']) @@ -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') @@ -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, @@ -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]); @@ -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' => [ diff --git a/Cake/Test/TestCase/ORM/Association/HasManyTest.php b/Cake/Test/TestCase/ORM/Association/HasManyTest.php index 1d0811219d4..ddaba7e6a84 100644 --- a/Cake/Test/TestCase/ORM/Association/HasManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasManyTest.php @@ -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)); @@ -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') @@ -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));