diff --git a/lib/Cake/ORM/Query.php b/lib/Cake/ORM/Query.php index c8d5814008f..4fd8ffa8893 100644 --- a/lib/Cake/ORM/Query.php +++ b/lib/Cake/ORM/Query.php @@ -785,8 +785,8 @@ protected function _addDefaultFields() { * @throws \BadMethodCallException */ public function __call($method, $args) { - array_unshift($args, $this); - return $this->repository()->__call($method, $args); + $options = array_shift($args) ?: []; + return $this->repository()->callFinder($method, $this, $options); } } diff --git a/lib/Cake/ORM/Table.php b/lib/Cake/ORM/Table.php index 22b7599d193..86fe7a8cede 100644 --- a/lib/Cake/ORM/Table.php +++ b/lib/Cake/ORM/Table.php @@ -660,6 +660,28 @@ public function deleteAll($conditions) { return $statement->rowCount() > 0; } +/** + * Calls a finder method directly and applies it to the passed query, + * if no query is passed a new one will be created and returned + * + * @param string $type name of the finder to be called + * @param \Cake\ORM\Query $query The query object to apply the finder options to + * @param array $args List of options to pass to the finder + * @return \Cake\ORM\Query + * @throws \BadMethodCallException + */ + public function callFinder($type, Query $query = null, $options = []) { + if (!method_exists($this, 'find' . ucfirst($type))) { + throw new \BadMethodCallException( + __d('cake_dev', 'Unknown table method %s', $type) + ); + } + if ($query === null) { + return $this->find($type, $options); + } + return $this->{'find' . ucfirst($type)}($query, $options); + } + /** * Magic method to be able to call scoped finders without the * find prefix @@ -670,16 +692,12 @@ public function deleteAll($conditions) { * @throws \BadMethodCallException */ public function __call($method, $args) { - if (method_exists($this, 'find' . ucfirst($method))) { - if (current($args) instanceof Query) { - $query = array_shift($args); - $options = current($args) ?: []; - return $this->{'find' . ucfirst($method)}($query, $options); - } - $options = current($args) ?: []; - return $this->find($method, $options); + $query = null; + if (isset($args[0]) && $args[0] instanceof Query) { + $query = array_shift($args); } - throw new \BadMethodCallException('Unknown table method ' . $method); + $options = array_shift($args) ?: []; + return $this->callFinder($method, $query, $options); } } diff --git a/lib/Cake/Test/TestCase/ORM/TableTest.php b/lib/Cake/Test/TestCase/ORM/TableTest.php index a3bc1bd20d3..7d12799ac3b 100644 --- a/lib/Cake/Test/TestCase/ORM/TableTest.php +++ b/lib/Cake/Test/TestCase/ORM/TableTest.php @@ -639,10 +639,12 @@ public function testStackingFinders() { $table->expects($this->once()) ->method('findList') - ->with($query) + ->with($query, ['keyPath' => 'id']) ->will($this->returnValue($query)); - - $result = $table->threaded(['order' => ['name' => 'ASC']])->list(); + + $result = $table + ->threaded(['order' => ['name' => 'ASC']]) + ->list(['keyPath' => 'id']); $this->assertSame($query, $result); }