Skip to content
Permalink
Browse files

Add query->clear().

I found a few duplicated blocks of code related to clearing query state
when subqueries are created. Having a function will help prevent code
getting out of sync in the future.
  • Loading branch information...
markstory committed Sep 4, 2014
1 parent 8691204 commit 13b224cf1c7adc9e976999ee85b31601702d8851
Showing with 27 additions and 11 deletions.
  1. +2 −5 src/ORM/Association/SelectableAssociationTrait.php
  2. +25 −6 src/ORM/Query.php
@@ -160,11 +160,8 @@ protected function _addFilteringCondition($query, $key, $filter) {
*/
protected function _buildSubquery($query) {
$filterQuery = clone $query;
$filterQuery->autoFields(false);
$filterQuery->limit(null);
$filterQuery->offset(null);
$filterQuery->order([], true);
$filterQuery->contain([], true);
$filterQuery->clear();
$joins = $filterQuery->join();
foreach ($joins as $i => $join) {
if (strtolower($join['type']) !== 'inner') {
@@ -467,19 +467,38 @@ public function applyOptions(array $options) {
return $this;
}
/**
* Clear many the clauses that will make cloned queries behave incorrectly.
*
* The following clauses/features will be cleared:
*
* - autoFields
* - limit
* - offset
* - map/reduce functions
* - result formatters
* - order
* - containments
*/
public function clear() {
$this->autoFields(false);
$this->limit(null);
$this->order([], true);
$this->offset(null);
$this->mapReduce(null, null, true);
$this->formatResults(null, true);
$this->contain([], true);
}
/**
* Returns the COUNT(*) for the query.
*
* @return int
*/
public function count() {
$query = clone $this;
$query->autoFields(false);
$query->limit(null);
$query->order([], true);
$query->offset(null);
$query->mapReduce(null, null, true);
$query->formatResults(null, true);
$query->clear();
$counter = $this->_counter;
if ($counter) {

2 comments on commit 13b224c

@spiliot

This comment has been minimized.

Copy link
Contributor

spiliot replied Sep 6, 2014

This commit seems to be breaking the following query in a controller:
$articles = $this->Articles->find()->where(['date' => $articleDate, 'Categories.title' => $catTitle]);
When $articles->count() is called the produced SQL doesn't include the Categories table and fails.

It was working up to this.

EDIT: The only difference between the old and new code is that the new one also clears containments.

@markstory

This comment has been minimized.

Copy link
Member Author

markstory replied Sep 6, 2014

Interesting, do you have contained assoiations?

Please sign in to comment.
You can’t perform that action at this time.