diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index c4efd8c2e..1a277613d 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -119,8 +119,9 @@ class OneToManyPersister extends AbstractCollectionPersister : $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]]; } + $filterTargetClass = $this->_em->getClassMetadata($targetClass->rootEntityName); foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) { - if ($filterExpr = $filter->addFilterConstraint($targetClass, 't')) { + if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) { $whereClauses[] = '(' . $filterExpr . ')'; } } diff --git a/tests/Doctrine/Tests/Models/Company/CompanyContract.php b/tests/Doctrine/Tests/Models/Company/CompanyContract.php index 70dd2fcb4..7787e96be 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyContract.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyContract.php @@ -21,7 +21,7 @@ abstract class CompanyContract private $id; /** - * @ManyToOne(targetEntity="CompanyEmployee") + * @ManyToOne(targetEntity="CompanyEmployee", inversedBy="soldContracts") */ private $salesPerson; diff --git a/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php b/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php index a9235c321..9d153770e 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php @@ -28,6 +28,11 @@ class CompanyEmployee extends CompanyPerson */ public $contracts; + /** + * @OneToMany(targetEntity="CompanyFlexUltraContract", mappedBy="salesPerson", fetch="EXTRA_LAZY") + */ + public $soldContracts; + public function getSalary() { return $this->salary; } diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 403bf3a04..7d3affd0e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -667,6 +667,9 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $contract1->addManager($manager2); + $contract1->setSalesPerson($manager); + $contract2->setSalesPerson($manager); + $this->_em->persist($manager); $this->_em->persist($manager2); $this->_em->persist($contract1); @@ -804,6 +807,59 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertFalse($contract->managers->isInitialized()); $this->assertEquals(1, count($contract->managers->slice(0, 10))); } + + public function testOneToMany_ExtraLazyCountWithFilterOnSTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertEquals(2, count($manager->soldContracts)); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertEquals(1, count($manager->soldContracts)); + } + + public function testOneToMany_ExtraLazyContainsWithFilterOnSTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + $contract1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyContract', $this->contractId1); + $contract2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyContract', $this->contractId2); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertTrue($manager->soldContracts->contains($contract1)); + $this->assertTrue($manager->soldContracts->contains($contract2)); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertFalse($manager->soldContracts->contains($contract1)); + $this->assertTrue($manager->soldContracts->contains($contract2)); + } + + public function testOneToMany_ExtraLazySliceWithFilterOnSTI() + { + + $this->loadCompanySingleTableInheritanceFixtureData(); + + $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertEquals(2, count($manager->soldContracts->slice(0, 10))); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->soldContracts->isInitialized()); + $this->assertEquals(1, count($manager->soldContracts->slice(0, 10))); + } } class MySoftDeleteFilter extends SQLFilter