From 1038a866a49e6d140d1e1670d8836dd6ceb0f3e0 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 5 Jun 2011 10:48:21 +0200 Subject: [PATCH] DDC-1194 - Improve error handling for DQL INSTANCE OF --- lib/Doctrine/ORM/Query/QueryException.php | 6 +++++ lib/Doctrine/ORM/Query/SqlWalker.php | 4 ++++ .../ORM/Query/SelectSqlGenerationTest.php | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/lib/Doctrine/ORM/Query/QueryException.php b/lib/Doctrine/ORM/Query/QueryException.php index aafe1e9d7..39dc42505 100644 --- a/lib/Doctrine/ORM/Query/QueryException.php +++ b/lib/Doctrine/ORM/Query/QueryException.php @@ -135,4 +135,10 @@ class QueryException extends \Doctrine\ORM\ORMException "in the query." ); } + + public static function instanceOfUnrelatedClass($className, $rootClass) + { + return new self("Cannot check if a child of '" . $rootClass . "' is instanceof '" . $className . "', " . + "inheritance hierachy exists between these two classes."); + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 611cd0a1d..270131ea2 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -1657,6 +1657,10 @@ class SqlWalker implements TreeWalker $sql .= $this->_conn->quote($class->discriminatorValue); } else { $discrMap = array_flip($class->discriminatorMap); + if (!isset($discrMap[$entityClassName])) { + throw QueryException::instanceOfUnrelatedClass($entityClassName, $class->rootEntityName); + } + $sql .= $this->_conn->quote($discrMap[$entityClassName]); } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 2d0101e03..6652ad67a 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -383,6 +383,28 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr = 'employee'" ); } + + /** + * @group DDC-1194 + */ + public function testSupportsInstanceOfExpressionsInWherePartPrefixedSlash() + { + $this->assertSqlGeneration( + "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\Company\CompanyEmployee", + "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr = 'employee'" + ); + } + + /** + * @group DDC-1194 + */ + public function testSupportsInstanceOfExpressionsInWherePartWithUnrelatedClass() + { + $this->assertInvalidSqlGeneration( + "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\CMS\CmsUser", + "Doctrine\ORM\Query\QueryException" + ); + } public function testSupportsInstanceOfExpressionsInWherePartInDeeperLevel() {