Skip to content
Permalink
Browse files

Refactoring to not call __call directly

  • Loading branch information...
lorenzo committed Aug 21, 2013
1 parent b2756db commit 39be9975230b35b89f9f12212c531a2cef34b834
Showing with 34 additions and 14 deletions.
  1. +2 −2 lib/Cake/ORM/Query.php
  2. +27 −9 lib/Cake/ORM/Table.php
  3. +5 −3 lib/Cake/Test/TestCase/ORM/TableTest.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);
}
}
@@ -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);
}
}
@@ -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);
}

0 comments on commit 39be997

Please sign in to comment.
You can’t perform that action at this time.