Skip to content

Commit

Permalink
Merge pull request #268 from cedriclombardot/fix-count-with-select-fr…
Browse files Browse the repository at this point in the history
…om-select

Mark as complex queries look like SELECT * FROM (SELECT * FROM book)
  • Loading branch information
willdurand committed Jan 28, 2012
2 parents ae7bdfc + 46dffc8 commit e95a5d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion runtime/lib/query/ModelCriteria.php
Expand Up @@ -1429,7 +1429,8 @@ protected function doCount($con)
|| $this->getOffset()
|| $this->getLimit()
|| $this->getHaving()
|| in_array(Criteria::DISTINCT, $this->getSelectModifiers());
|| in_array(Criteria::DISTINCT, $this->getSelectModifiers())
|| count($this->selectQueries) > 0;

$params = array();
if ($needsComplexCount) {
Expand Down
18 changes: 17 additions & 1 deletion test/testsuite/runtime/query/SubQueryTest.php
Expand Up @@ -219,6 +219,22 @@ public function testSubQueryWithSelectColumns()
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'addSelectQuery() forges a unique alias and adds select columns by default');
}

public function testSubQueryCount()
{
$subCriteria = new BookQuery();

$c = new BookQuery();
$c->addSelectQuery($subCriteria, 'subCriteriaAlias');
$c->filterByPrice(20, Criteria::LESS_THAN);
$nbBooks = $c->count();

$query = Propel::getConnection()->getLastExecutedQuery();

$sql = "SELECT COUNT(*) FROM (SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<20) propelmatch4cnt";

$this->assertEquals($sql, $query, 'addSelectQuery() doCount is defined as complexQuery');
}
}

class TestableBookQuery extends BookQuery
Expand All @@ -227,4 +243,4 @@ public function configureSelectColumns()
{
return parent::configureSelectColumns();
}
}
}

0 comments on commit e95a5d4

Please sign in to comment.