1
0
mirror of synced 2025-02-20 14:13:15 +03:00
This commit is contained in:
zYne 2007-06-27 18:42:47 +00:00
parent 8ae8fa7c40
commit 2a2cb285a0
3 changed files with 49 additions and 3 deletions

View File

@ -96,6 +96,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
*/
protected $_pendingJoinConditions = array();
protected $_expressionMap = array();
protected $_state = Doctrine_Query::STATE_CLEAN;
/**
@ -115,6 +117,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$this->pendingSubqueries = array();
$this->pendingFields = array();
$this->_neededTables = array();
$this->_expressionMap = array();
$this->subqueryAliases = array();
$this->needsSubquery = false;
$this->isLimitSubqueryUsed = false;
@ -228,6 +231,9 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
public function getAggregateAlias($dqlAlias)
{
if (isset($this->aggregateMap[$dqlAlias])) {
// mark the expression as used
$this->_expressionMap[$dqlAlias][1] = true;
return $this->aggregateMap[$dqlAlias];
}
if ( ! empty($this->pendingAggregates)) {
@ -577,6 +583,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$this->parts['select'][] = $expression . ' AS ' . $sqlAlias;
$this->aggregateMap[$alias] = $sqlAlias;
$this->_expressionMap[$alias][0] = $expression;
$this->_aliasMap[$componentAlias]['agg'][$index] = $alias;
@ -852,7 +859,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
/**
foreach ($this->parts['orderby'] as $part) {
$part = trim($part);
$e = Doctrine_Tokenizer::bracketExplode($part, ' ');
@ -884,6 +891,14 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
}
}
}
*/
if ($driverName == 'mysql' || $driverName == 'pgsql') {
foreach ($this->_expressionMap as $dqlAlias => $expr) {
if (isset($expr[1])) {
$subquery .= ', ' . $expr[0] . ' AS ' . $this->aggregateMap[$dqlAlias];
}
}
}
$subquery .= ' FROM';
@ -928,7 +943,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if (strpos($part, '.') !== false) {
$separator = '.';
}
if ($driverName == 'mysql' || $driverName == 'pgsql') {
if (strpos($part, '__') !== false) {
$separator = '__';

View File

@ -69,7 +69,7 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition
$func = $this->query->getTableAlias($reference) . '.' . $field;
} else {
$field = end($a);
$func = $this->query->getAggregateAlias($field);
$func = $this->query->getAggregateAlias($field);
}
return $func;
} else {

View File

@ -83,4 +83,35 @@ class Doctrine_Query_MysqlSubquery_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0, e2.name LIMIT 5');
}
public function testGetLimitSubquerySupportsOrderByAndHavingWithAggregateValues()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->orderby('num_albums DESC');
$q->having('num_albums > 0');
$q->groupby('u.id');
$q->limit(5);
$q->execute();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id HAVING a2__0 > 0 ORDER BY a2__0 DESC LIMIT 5');
}
public function testGetLimitSubquerySupportsHavingWithAggregateValues()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->having('num_albums > 0');
$q->groupby('u.id');
$q->limit(5);
$q->execute();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id HAVING a2__0 > 0 LIMIT 5');
}
}