diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index d57ae0a36..da9d4027d 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -307,7 +307,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } else { $this->setQueryPart($queryPartName, $sql); } - } + } } $this->_state = Doctrine_Query::STATE_DIRTY; @@ -696,11 +696,11 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable */ public function getQuery($params = array()) { - if ($this->_state !== self::STATE_DIRTY) { - return $this->_sql; - } + if ($this->_state !== self::STATE_DIRTY) { + return $this->_sql; + } - $parts = $this->_dqlParts; + $parts = $this->_dqlParts; // reset the state $this->_aliasMap = array(); @@ -928,16 +928,51 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $subquery = $this->_conn->modifyLimitQuery($subquery, $this->parts['limit'], $this->parts['offset']); $parts = Doctrine_Tokenizer::quoteExplode($subquery, ' ', "'", "'"); - + //print_r($parts); foreach ($parts as $k => $part) { if (strpos($part, "'") !== false) { continue; } - + if ($this->hasTableAlias($part)) { $parts[$k] = $this->generateNewTableAlias($part); + continue; } + + if (strpos($part, '.') == false) { + continue; + } + preg_match_all("/[a-zA-Z0-9_]+\.[a-z0-9_]+/i", $part, $m); + foreach ($m[0] as $match) { + $e = explode('.', $match); + $e[0] = $this->generateNewTableAlias($e[0]); + + $parts[$k] = str_replace($match, implode('.', $e), $parts[$k]); + } + } + + if ($driverName == 'mysql' || $driverName == 'pgsql') { + foreach ($parts as $k => $part) { + if (strpos($part, "'") !== false) { + continue; + } + if (strpos($part, '__') == false) { + continue; + } + + preg_match_all("/[a-zA-Z0-9_]+\_\_[a-z0-9_]+/i", $part, $m); + + foreach ($m[0] as $match) { + $e = explode('__', $match); + $e[0] = $this->generateNewTableAlias($e[0]); + + $parts[$k] = str_replace($match, implode('__', $e), $parts[$k]); + } + } + } + /** + $separator = false; if (strpos($part, '.') !== false) { @@ -958,8 +993,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $e[0] = substr($e[0], 0, $pos) . $this->generateNewTableAlias($trimmed); $parts[$k] = implode($separator, $e); - } - } + }*/ $subquery = implode(' ', $parts); return $subquery; diff --git a/tests/Query/MysqlSubqueryHavingTestCase.php b/tests/Query/MysqlSubqueryHavingTestCase.php index a2a3659af..e9d6484ed 100644 --- a/tests/Query/MysqlSubqueryHavingTestCase.php +++ b/tests/Query/MysqlSubqueryHavingTestCase.php @@ -52,7 +52,7 @@ class Doctrine_Query_MysqlSubqueryHaving_TestCase extends Doctrine_UnitTestCase $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 LIMIT 5'); + $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 testGetLimitSubqueryWithHavingOnAggregateValuesIncorrectAlias() @@ -68,7 +68,6 @@ class Doctrine_Query_MysqlSubqueryHaving_TestCase extends Doctrine_UnitTestCase $q->execute(); $this->dbh->pop(); - - $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(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 LIMIT 5'); + $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(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'); } -} \ No newline at end of file +} diff --git a/tests/run.php b/tests/run.php index 918397b36..749e1ad90 100644 --- a/tests/run.php +++ b/tests/run.php @@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); $test->addTestCase(new Doctrine_Ticket330_TestCase()); */ - /** */ + /** */ // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase());