Permalink
Browse files

Moved more code that can be used genericaly by any query

  • Loading branch information...
1 parent d6ab39b commit 7d6cc7e24bff90637a22cb1c5c9c35fe85668d2c @lorenzo lorenzo committed Feb 20, 2014
Showing with 72 additions and 40 deletions.
  1. +68 −3 src/Datasource/QueryTrait.php
  2. +4 −37 src/ORM/Query.php
@@ -68,6 +68,14 @@
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.
*
@@ -310,6 +318,53 @@ public function 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
*
* @return \Traversable
@@ -323,23 +378,33 @@ public function first() {
* @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';
+ }
+
}
View
@@ -32,6 +32,7 @@ class Query extends DatabaseQuery {
use QueryTrait {
cache as private _cache;
all as private _all;
+ __call as private _call;
}
/**
@@ -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
@@ -463,25 +457,6 @@ public function applyOptions(array $options) {
}
/**
- * 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.
*
* @return integer
@@ -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())
);
}

0 comments on commit 7d6cc7e

Please sign in to comment.