From a0ca506db7e577c55dd89544bfd8ba08962c3cfe Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Sun, 14 Aug 2011 00:46:02 -0300 Subject: [PATCH] Added support to SingleValuePathExpression in ORDER BY. --- lib/Doctrine/ORM/Query/Parser.php | 7 ++---- lib/Doctrine/ORM/Query/SqlWalker.php | 24 +++++++++---------- .../ORM/Query/SelectSqlGenerationTest.php | 19 ++++++++++++++- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index afb1353dd..0f7c88538 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -1342,10 +1342,7 @@ class Parser } /** - * OrderByItem ::= (ResultVariable | StateFieldPathExpression) ["ASC" | "DESC"] - * - * @todo Post 2.0 release. Support general SingleValuedPathExpression instead - * of only StateFieldPathExpression. + * OrderByItem ::= (ResultVariable | SingleValuedPathExpression) ["ASC" | "DESC"] * * @return \Doctrine\ORM\Query\AST\OrderByItem */ @@ -1360,7 +1357,7 @@ class Parser $token = $this->_lexer->lookahead; $expr = $this->ResultVariable(); } else { - $expr = $this->StateFieldPathExpression(); + $expr = $this->SingleValuedPathExpression(); } $item = new AST\OrderByItem($expr); diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 33c9f45ea..cc387543c 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -479,20 +479,20 @@ class SqlWalker implements TreeWalker $assoc = $class->associationMappings[$fieldName]; - if ($assoc['isOwningSide']) { - // COMPOSITE KEYS NOT (YET?) SUPPORTED - if (count($assoc['sourceToTargetKeyColumns']) > 1) { - throw QueryException::associationPathCompositeKeyNotSupported(); - } - - if ($this->_useSqlTableAliases) { - $sql .= $this->getSQLTableAlias($class->table['name'], $dqlAlias) . '.'; - } - - $sql .= reset($assoc['targetToSourceKeyColumns']); - } else { + if ( ! $assoc['isOwningSide']) { throw QueryException::associationPathInverseSideNotSupported(); } + + // COMPOSITE KEYS NOT (YET?) SUPPORTED + if (count($assoc['sourceToTargetKeyColumns']) > 1) { + throw QueryException::associationPathCompositeKeyNotSupported(); + } + + if ($this->_useSqlTableAliases) { + $sql .= $this->getSQLTableAlias($class->table['name'], $dqlAlias) . '.'; + } + + $sql .= reset($assoc['targetToSourceKeyColumns']); break; default: diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 22e15785b..503807594 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -39,7 +39,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $query->setHint($name, $value); } - parent::assertEquals($sqlToBeConfirmed, $query->getSql()); + parent::assertEquals($sqlToBeConfirmed, $query->getSQL()); $query->free(); } catch (\Exception $e) { $this->fail($e->getMessage() ."\n".$e->getTraceAsString()); @@ -678,6 +678,23 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT COUNT(*) FROM cms_articles c1_ WHERE c1_.user_id = c0_.id) AS sclr4 FROM cms_users c0_ ORDER BY sclr4 ASC" ); } + + public function testOrderBySupportsSingleValuedPathExpressionOwningSide() + { + $this->assertSqlGeneration( + "select a from Doctrine\Tests\Models\CMS\CmsArticle a order by a.user", + "SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2, c0_.version AS version3 FROM cms_articles c0_ ORDER BY c0_.user_id ASC" + ); + } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + */ + public function testOrderBySupportsSingleValuedPathExpressionInverseSide() + { + $q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u order by u.address"); + $q->getSQL(); + } public function testBooleanLiteralInWhereOnSqlite() {