Skip to content

Commit

Permalink
Refactoring to not call __call directly
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 21, 2013
1 parent b2756db commit 39be997
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/ORM/Query.php
Expand Up @@ -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);
}

}
36 changes: 27 additions & 9 deletions lib/Cake/ORM/Table.php
Expand Up @@ -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
Expand All @@ -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);
}

}
8 changes: 5 additions & 3 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -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);
}

Expand Down

0 comments on commit 39be997

Please sign in to comment.