From 752b5023260527dcf458e0b66778518a22f90096 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 5 Dec 2011 18:26:56 +0100 Subject: [PATCH] [DDC-551] Add filters only on root entities in JoinedSubclassPersister --- .../Persisters/JoinedSubclassPersister.php | 17 +++++++ .../Tests/ORM/Functional/SQLFilterTest.php | 50 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 190ae7be6..9e9477f0a 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -327,6 +327,14 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister if ($first) $first = false; else $joinSql .= ' AND '; $joinSql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; + + if($parentClass->name === $this->_class->rootEntityName) { + // Add filters on the root class + $filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias); + if('' !== $filterSql) { + $joinSql .= ' AND ' . $filterSql; + } + } } } @@ -374,6 +382,15 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $conditionSql = $this->_getSelectConditionSQL($criteria, $assoc); + // If the current class in the root entity, add the filters + if($this->_class->name === $this->_class->rootEntityName) { + $filterSql = $this->generateFilterConditionSQL($this->_class, $baseTableAlias); + if('' !== $filterSql) { + if($conditionSql) $conditionSql .= ' AND '; + $conditionSql .= $filterSql; + } + } + $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : ''; diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 107b4ca84..5d9803277 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -513,6 +513,40 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->groupId2 = $group2->id; } + public function testJoinSubclassPersister_FilterOnlyOnRootTableWhenFetchingSubEntity() + { + $this->loadCompanyFixtureData(); + $this->assertEquals(2, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll())); + + // Enable the filter + $conf = $this->_em->getConfiguration(); + $conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter"); + $this->_em->getFilters() + ->enable("person_name") + ->setParameter("name", "Guilh%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType()); + + $managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll(); + $this->assertEquals(1, count($managers)); + $this->assertEquals("Guilherme", $managers[0]->getName()); + } + + public function testJoinSubclassPersister_FilterOnlyOnRootTableWhenFetchingRootEntity() + { + $this->loadCompanyFixtureData(); + $this->assertEquals(3, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll())); + + // Enable the filter + $conf = $this->_em->getConfiguration(); + $conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter"); + $this->_em->getFilters() + ->enable("person_name") + ->setParameter("name", "Guilh%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType()); + + $managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll(); + $this->assertEquals(1, count($managers)); + $this->assertEquals("Guilherme", $managers[0]->getName()); + } + private function loadCompanyFixtureData() { $manager = new CompanyManager; @@ -527,8 +561,12 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $manager2->setSalary(42); $manager2->setDepartment('parsers'); + $person = new CompanyPerson; + $person->setName('Benjamin'); + $this->_em->persist($manager); $this->_em->persist($manager2); + $this->_em->persist($person); $this->_em->flush(); $this->_em->clear(); } @@ -592,3 +630,15 @@ class CMSArticleTopicFilter extends SQLFilter return $targetTableAlias.'.topic = ' . $this->getParameter('topic'); // getParam uses connection to quote the value. } } + +class CompanyPersonNameFilter extends SQLFilter +{ + public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias, $targetTable = '') + { + if ($targetEntity->name != "Doctrine\Tests\Models\Company\CompanyPerson") { + return ""; + } + + return $targetTableAlias.'.name LIKE ' . $this->getParameter('name'); + } +}