Skip to content

Commit 13b224c

Browse files
committed
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.
1 parent 8691204 commit 13b224c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/ORM/Association/SelectableAssociationTrait.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,8 @@ protected abstract function _linkField($options);
160160
*/
161161
protected function _buildSubquery($query) {
162162
$filterQuery = clone $query;
163-
$filterQuery->autoFields(false);
164-
$filterQuery->limit(null);
165-
$filterQuery->offset(null);
166-
$filterQuery->order([], true);
167-
$filterQuery->contain([], true);
163+
$filterQuery->clear();
164+
168165
$joins = $filterQuery->join();
169166
foreach ($joins as $i => $join) {
170167
if (strtolower($join['type']) !== 'inner') {

src/ORM/Query.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,19 +467,38 @@ public function applyOptions(array $options) {
467467
return $this;
468468
}
469469

470+
/**
471+
* Clear many the clauses that will make cloned queries behave incorrectly.
472+
*
473+
* The following clauses/features will be cleared:
474+
*
475+
* - autoFields
476+
* - limit
477+
* - offset
478+
* - map/reduce functions
479+
* - result formatters
480+
* - order
481+
* - containments
482+
*/
483+
public function clear() {
484+
$this->autoFields(false);
485+
$this->limit(null);
486+
$this->order([], true);
487+
$this->offset(null);
488+
$this->mapReduce(null, null, true);
489+
$this->formatResults(null, true);
490+
$this->contain([], true);
491+
}
492+
470493
/**
471494
* Returns the COUNT(*) for the query.
472495
*
473496
* @return int
474497
*/
475498
public function count() {
476499
$query = clone $this;
477-
$query->autoFields(false);
478-
$query->limit(null);
479-
$query->order([], true);
480-
$query->offset(null);
481-
$query->mapReduce(null, null, true);
482-
$query->formatResults(null, true);
500+
$query->clear();
501+
483502
$counter = $this->_counter;
484503

485504
if ($counter) {

0 commit comments

Comments
 (0)