Skip to content

Commit

Permalink
Removing ORM specific code out of QueryTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 21, 2014
1 parent 06d6343 commit f2cd735
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Datasource/QueryCacher.php
Expand Up @@ -22,9 +22,9 @@
/**
* Handles caching queries and loading results from the cache.
*
* Used by Cake\ORM\Query internally.
* Used by Cake\Datasource\QueryTrait internally.
*
* @see Cake\ORM\Query::cache() for the public interface.
* @see Cake\Datasource\QueryTrait::cache() for the public interface.
*/
class QueryCacher {

Expand Down
19 changes: 5 additions & 14 deletions src/Datasource/QueryTrait.php
Expand Up @@ -40,7 +40,7 @@ trait QueryTrait {
*
* When set, query execution will be bypassed.
*
* @var Cake\ORM\ResultSet
* @var \Cake\Datasource\ResultSetDecorator
* @see setResult()
*/
protected $_results;
Expand Down Expand Up @@ -186,13 +186,9 @@ public function getResults() {
* When using a function, this query instance will be supplied as an argument.
* @param string|CacheEngine $config Either the name of the cache config to use, or
* a cache config instance.
* @return Query The query instance.
* @throws \RuntimeException When you attempt to cache a non-select query.
* @return QueryTrait This same object
*/
public function cache($key, $config = 'default') {
if ($this->_type !== 'select' && $this->_type !== null) {
throw new \RuntimeException('You cannot cache the results of non-select queries.');
}
if ($key === false) {
$this->_cache = null;
return $this;
Expand All @@ -205,20 +201,15 @@ public function cache($key, $config = 'default') {
* Fetch the results for this query.
*
* Compiles the SQL representation of this query and executes it using the
* provided connection object. Returns a ResultSet iterator object.
* provided connection object. Returns a ResultSetDecorator iterator object.
*
* ResultSet is a travesable object that implements the methods found
* ResultSetDecorator is a travesable object that implements the methods found
* on Cake\Collection\Collection.
*
* @return Cake\ORM\ResultCollectionTrait
* @return Cake\ORM\ResultSetDecorator
* @throws RuntimeException if this method is called on a non-select Query.
*/
public function all() {
if ($this->_type !== 'select' && $this->_type !== null) {
throw new \RuntimeException(
'You cannot call all() on a non-select query. Use execute() instead.'
);
}
return $this->getResults();
}

Expand Down
32 changes: 31 additions & 1 deletion src/ORM/Query.php
Expand Up @@ -28,7 +28,10 @@
*/
class Query extends DatabaseQuery {

use QueryTrait;
use QueryTrait {
cache as private _cache;
all as private _all;
}

/**
* Indicates that the operation should append to the list
Expand Down Expand Up @@ -553,6 +556,33 @@ public function hydrate($enable = null) {
return $this;
}

/**
* {@inheritdoc}
*
* @return Query The query instance.
* @throws \RuntimeException When you attempt to cache a non-select query.
*/
public function cache($key, $config = 'default') {
if ($this->_type !== 'select' && $this->_type !== null) {
throw new \RuntimeException('You cannot cache the results of non-select queries.');
}
return $this->_cache($key, $config);
}

/**
* {@inheritdoc}
*
* @throws RuntimeException if this method is called on a non-select Query.
*/
public function all() {
if ($this->_type !== 'select' && $this->_type !== null) {
throw new \RuntimeException(
'You cannot call all() on a non-select query. Use execute() instead.'
);
}
return $this->_all();
}

/**
* Auxiliary function used to wrap the original statement from the driver with
* any registered callbacks. This will also setup the correct statement class
Expand Down

0 comments on commit f2cd735

Please sign in to comment.