Skip to content

Commit 39be997

Browse files
committed
Refactoring to not call __call directly
1 parent b2756db commit 39be997

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

lib/Cake/ORM/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ protected function _addDefaultFields() {
785785
* @throws \BadMethodCallException
786786
*/
787787
public function __call($method, $args) {
788-
array_unshift($args, $this);
789-
return $this->repository()->__call($method, $args);
788+
$options = array_shift($args) ?: [];
789+
return $this->repository()->callFinder($method, $this, $options);
790790
}
791791

792792
}

lib/Cake/ORM/Table.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,28 @@ public function deleteAll($conditions) {
660660
return $statement->rowCount() > 0;
661661
}
662662

663+
/**
664+
* Calls a finder method directly and applies it to the passed query,
665+
* if no query is passed a new one will be created and returned
666+
*
667+
* @param string $type name of the finder to be called
668+
* @param \Cake\ORM\Query $query The query object to apply the finder options to
669+
* @param array $args List of options to pass to the finder
670+
* @return \Cake\ORM\Query
671+
* @throws \BadMethodCallException
672+
*/
673+
public function callFinder($type, Query $query = null, $options = []) {
674+
if (!method_exists($this, 'find' . ucfirst($type))) {
675+
throw new \BadMethodCallException(
676+
__d('cake_dev', 'Unknown table method %s', $type)
677+
);
678+
}
679+
if ($query === null) {
680+
return $this->find($type, $options);
681+
}
682+
return $this->{'find' . ucfirst($type)}($query, $options);
683+
}
684+
663685
/**
664686
* Magic method to be able to call scoped finders without the
665687
* find prefix
@@ -670,16 +692,12 @@ public function deleteAll($conditions) {
670692
* @throws \BadMethodCallException
671693
*/
672694
public function __call($method, $args) {
673-
if (method_exists($this, 'find' . ucfirst($method))) {
674-
if (current($args) instanceof Query) {
675-
$query = array_shift($args);
676-
$options = current($args) ?: [];
677-
return $this->{'find' . ucfirst($method)}($query, $options);
678-
}
679-
$options = current($args) ?: [];
680-
return $this->find($method, $options);
695+
$query = null;
696+
if (isset($args[0]) && $args[0] instanceof Query) {
697+
$query = array_shift($args);
681698
}
682-
throw new \BadMethodCallException('Unknown table method ' . $method);
699+
$options = array_shift($args) ?: [];
700+
return $this->callFinder($method, $query, $options);
683701
}
684702

685703
}

lib/Cake/Test/TestCase/ORM/TableTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,12 @@ public function testStackingFinders() {
639639

640640
$table->expects($this->once())
641641
->method('findList')
642-
->with($query)
642+
->with($query, ['keyPath' => 'id'])
643643
->will($this->returnValue($query));
644-
645-
$result = $table->threaded(['order' => ['name' => 'ASC']])->list();
644+
645+
$result = $table
646+
->threaded(['order' => ['name' => 'ASC']])
647+
->list(['keyPath' => 'id']);
646648
$this->assertSame($query, $result);
647649
}
648650

0 commit comments

Comments
 (0)