Navigation Menu

Skip to content

Commit

Permalink
Adding magic __call() methods in order to call finders directly and
Browse files Browse the repository at this point in the history
stack them
  • Loading branch information
lorenzo committed Aug 20, 2013
1 parent 1ae0413 commit 16020a4
Show file tree
Hide file tree
Showing 5 changed files with 894 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Cake/ORM/Query.php
Expand Up @@ -776,4 +776,17 @@ protected function _addDefaultFields() {
$this->select($aliased, true); $this->select($aliased, true);
} }


/**
* Magic method to be able to call scoped finders in the default table
*
* @param string $method name of the method to be invoked
* @param array $args List of arguments passed to the function
* @return mixed
* @throws \BadMethodCallException
*/
public function __call($method, $args) {
array_unshift($args, $this);
return $this->repository()->__call($method, $args);
}

} }
22 changes: 22 additions & 0 deletions lib/Cake/ORM/Table.php
Expand Up @@ -660,4 +660,26 @@ public function deleteAll($conditions) {
return $statement->rowCount() > 0; return $statement->rowCount() > 0;
} }


/**
* Magic method to be able to call scoped finders without the
* find prefix
*
* @param string $method name of the method to be invoked
* @param array $args List of arguments passed to the function
* @return mixed
* @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);
}
throw new \BadMethodCallException('Unknown table method ' . $method);
}

} }
1 change: 1 addition & 0 deletions lib/Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -1003,4 +1003,5 @@ function($k, $v, $mr) { $mr->emit($v[0] + 1); }


$this->assertEquals([2, 3], iterator_to_array($query->execute())); $this->assertEquals([2, 3], iterator_to_array($query->execute()));
} }

} }

0 comments on commit 16020a4

Please sign in to comment.