Skip to content

Commit

Permalink
Moved more code that can be used genericaly by any query
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 21, 2014
1 parent d6ab39b commit 7d6cc7e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 40 deletions.
71 changes: 68 additions & 3 deletions src/Datasource/QueryTrait.php
Expand Up @@ -67,6 +67,14 @@ trait QueryTrait {
*/
protected $_cache;

/**
* Holds any custom options passed using applyOptions that could not be processed
* by any method in this class.
*
* @var array
*/
protected $_options = [];

/**
* Returns the default table object that will be used by this query,
* that is, the table that will appear in the from clause.
Expand Down Expand Up @@ -309,6 +317,53 @@ public function first() {
return $this->all()->first();
}

/**
* Returns an array with the custom options that were applied to this query
* and that were not already processed by another method in this class.
*
* ###Example:
*
* {{{
* $query->applyOptions(['doABarrelRoll' => true, 'fields' => ['id', 'name']);
* $query->getOptions(); // Returns ['doABarrelRoll' => true]
* }}}
*
* @see \Cake\ORM\Query::applyOptions() to read about the options that will
* be processed by this class and not returned by this function
* @return array
*/
public function getOptions() {
return $this->_options;
}

/**
* Enables calling methods from the result set as if they were from this class
*
* @param string $method the method to call
* @param array $arguments list of arguments for the method to call
* @return mixed
* @throws \BadMethodCallException if no such method exists in result set
*/
public function __call($method, $arguments) {
$resultSetClass = $this->_decoratorClass();
if (in_array($method, get_class_methods($resultSetClass))) {
$results = $this->all();
return call_user_func_array([$results, $method], $arguments);
}
throw new \BadMethodCallException(
sprintf('Unknown method "%s"', $method)
);
}

/**
* Populates or adds parts to current query clauses using an array.
* This is handy for passing all query clauses at once.
*
* @param array $options the options to be applied
* @return Cake\Datasource\QueryTrait this object
*/
abstract public function applyOptions(array $options);

/**
* Executes this query and returns a traversable object containing the results
*
Expand All @@ -323,23 +378,33 @@ abstract protected function _execute();
* @return \Cake\Datasoruce\ResultSetDecorator
*/
protected function _decorateResults($result) {
$decorator = $this->_decoratorClass();
foreach ($this->_mapReduce as $functions) {
$result = new MapReduce($result, $functions['mapper'], $functions['reducer']);
}

if (!empty($this->_mapReduce)) {
$result = new ResultSetDecorator($result);
$result = new $decorator($result);
}

foreach ($this->_formatters as $formatter) {
$result = $formatter($result, $this);
}

if (!empty($this->_formatters) && !($result instanceof ResultSetDecorator)) {
$result = new ResultSetDecorator($result);
if (!empty($this->_formatters) && !($result instanceof $decorator)) {
$result = new $decorator($result);
}

return $result;
}

/**
* Returns the name of the class to be used for decorating results
*
* @return string
*/
protected function _decoratorClass() {
return 'Cake\Datasource\ResultSetDecorator';
}

}
41 changes: 4 additions & 37 deletions src/ORM/Query.php
Expand Up @@ -32,6 +32,7 @@ class Query extends DatabaseQuery {
use QueryTrait {
cache as private _cache;
all as private _all;
__call as private _call;
}

/**
Expand Down Expand Up @@ -71,13 +72,6 @@ class Query extends DatabaseQuery {
*/
protected $_useBufferedResults = true;

/**
* Holds any custom options passed using applyOptions that could not be processed
* by any method in this class.
*
* @var array
*/
protected $_options = [];

/**
* Whether to hydrate results into entity objects
Expand Down Expand Up @@ -462,25 +456,6 @@ public function applyOptions(array $options) {
return $this;
}

/**
* Returns an array with the custom options that were applied to this query
* and that were not already processed by another method in this class.
*
* ###Example:
*
* {{{
* $query->applyOptions(['doABarrelRoll' => true, 'fields' => ['id', 'name']);
* $query->getOptions(); // Returns ['doABarrelRoll' => true]
* }}}
*
* @see \Cake\ORM\Query::applyOptions() to read about the options that will
* be processed by this class and not returned by this function
* @return array
*/
public function getOptions() {
return $this->_options;
}

/**
* Return the COUNT(*) for for the query.
*
Expand Down Expand Up @@ -730,24 +705,16 @@ public function insert($columns, $types = []) {
}

/**
* Enables calling methods from the ResultSet as if they were from this class
* {@inheritdoc}
*
* @param string $method the method to call
* @param array $arguments list of arguments for the method to call
* @return mixed
* @throws \BadMethodCallException if no such method exists in ResultSet
*/
public function __call($method, $arguments) {
if ($this->type() === 'select') {
$resultSetClass = 'Cake\Datasource\ResultSetDecorator';
if (in_array($method, get_class_methods($resultSetClass))) {
$results = $this->all();
return call_user_func_array([$results, $method], $arguments);
}
return $this->_call($method, $arguments);
}

throw new \BadMethodCallException(
sprintf('Unknown method "%s"', $method)
sprintf('Cannot call method "%s" on a "%s" query', $method, $this->type())
);
}

Expand Down

0 comments on commit 7d6cc7e

Please sign in to comment.