From ca470d8ba765d8b383f735eeef7f66d9560f9965 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 21 Dec 2011 00:00:43 +0100 Subject: [PATCH 1/9] Fix glitch in Version produced by build-script --- lib/Doctrine/ORM/Version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Version.php b/lib/Doctrine/ORM/Version.php index a8bfd4eff..e8124cd45 100644 --- a/lib/Doctrine/ORM/Version.php +++ b/lib/Doctrine/ORM/Version.php @@ -36,7 +36,7 @@ class Version /** * Current Doctrine Version */ - const VERSION = '2.2.01-DEV'; + const VERSION = '2.2.0-DEV'; /** * Compares a Doctrine version with the current one. From 62be27b295e2363a455e15f43645c6f180aa491a Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 21 Dec 2011 23:56:17 +0100 Subject: [PATCH 2/9] Added tests for ManyToMany associations and lazy collection to STI entity --- .../ORM/Persisters/ManyToManyPersister.php | 20 +++-- .../Tests/Models/Company/CompanyContract.php | 4 +- .../Tests/Models/Company/CompanyEmployee.php | 7 +- .../Models/Company/CompanyFlexContract.php | 25 +++++- .../Tests/Models/Company/CompanyManager.php | 7 +- .../Tests/ORM/Functional/SQLFilterTest.php | 78 +++++++++++++++++++ 6 files changed, 130 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index 34498a325..33dab21d1 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -192,7 +192,7 @@ class ManyToManyPersister extends AbstractCollectionPersister */ public function count(PersistentCollection $coll) { - $mapping = $coll->getMapping(); + $mapping = $filterMapping = $coll->getMapping(); $class = $this->_em->getClassMetadata($mapping['sourceEntity']); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); @@ -218,7 +218,7 @@ class ManyToManyPersister extends AbstractCollectionPersister : $id[$class->fieldNames[$joinColumns[$joinTableColumn]]]; } - list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping); + list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping); if ($filterSql) { $whereClauses[] = $filterSql; } @@ -295,7 +295,7 @@ class ManyToManyPersister extends AbstractCollectionPersister private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters) { $uow = $this->_em->getUnitOfWork(); - $mapping = $coll->getMapping(); + $mapping = $filterMapping = $coll->getMapping(); if ( ! $mapping['isOwningSide']) { $sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']); @@ -332,7 +332,7 @@ class ManyToManyPersister extends AbstractCollectionPersister } if ($addFilters) { - list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping); + list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping); if ($filterSql) { $quotedJoinTable .= ' t ' . $joinTargetEntitySQL; $whereClauses[] = $filterSql; @@ -351,13 +351,21 @@ class ManyToManyPersister extends AbstractCollectionPersister * have to join in the actual entities table leading to additional * JOIN. * - * @param array $targetEntity Array containing mapping information. + * @param array $mapping Array containing mapping information. * * @return string The SQL query part to add to a query. */ public function getFilterSql($mapping) { $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); + + if ($mapping['isOwningSide']) { + $joinColumns = $mapping['relationToTargetKeyColumns']; + } else { + $mapping = $targetClass->associationMappings[$mapping['mappedBy']]; + $joinColumns = $mapping['relationToSourceKeyColumns']; + } + $targetClass = $this->_em->getClassMetadata($targetClass->rootEntityName); // A join is needed if there is filtering on the target entity @@ -368,7 +376,7 @@ class ManyToManyPersister extends AbstractCollectionPersister . ' ON'; $joinTargetEntitySQLClauses = array(); - foreach ($mapping['relationToTargetKeyColumns'] as $joinTableColumn => $targetTableColumn) { + foreach ($joinColumns as $joinTableColumn => $targetTableColumn) { $joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn; } diff --git a/tests/Doctrine/Tests/Models/Company/CompanyContract.php b/tests/Doctrine/Tests/Models/Company/CompanyContract.php index 221bf1cd8..70dd2fcb4 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyContract.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyContract.php @@ -32,7 +32,7 @@ abstract class CompanyContract private $completed = false; /** - * @ManyToMany(targetEntity="CompanyEmployee") + * @ManyToMany(targetEntity="CompanyEmployee", inversedBy="contracts") * @JoinTable(name="company_contract_employees", * joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")}, * inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")} @@ -86,4 +86,4 @@ abstract class CompanyContract } abstract public function calculatePrice(); -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php b/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php index 5e050f948..a9235c321 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyEmployee.php @@ -23,6 +23,11 @@ class CompanyEmployee extends CompanyPerson */ private $startDate; + /** + * @ManyToMany(targetEntity="CompanyContract", mappedBy="engineers", fetch="EXTRA_LAZY") + */ + public $contracts; + public function getSalary() { return $this->salary; } @@ -46,4 +51,4 @@ class CompanyEmployee extends CompanyPerson public function setStartDate($date) { $this->startDate = $date; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php b/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php index 11f966f17..976506d7c 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php @@ -18,6 +18,15 @@ class CompanyFlexContract extends CompanyContract */ private $pricePerHour = 0; + /** + * @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts") + * @JoinTable(name="company_contract_managers", + * joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")}, + * inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")} + * ) + */ + private $managers; + public function calculatePrice() { return $this->hoursWorked * $this->pricePerHour; @@ -42,4 +51,18 @@ class CompanyFlexContract extends CompanyContract { $this->pricePerHour = $pricePerHour; } -} \ No newline at end of file + public function getManagers() + { + return $this->managers; + } + + public function addManager(CompanyManager $manager) + { + $this->managers[] = $manager; + } + + public function removeManager(CompanyManager $manager) + { + $this->managers->removeElement($manager); + } +} diff --git a/tests/Doctrine/Tests/Models/Company/CompanyManager.php b/tests/Doctrine/Tests/Models/Company/CompanyManager.php index e0d39dfcf..aec9a77ae 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyManager.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyManager.php @@ -19,6 +19,11 @@ class CompanyManager extends CompanyEmployee */ private $car; + /** + * @ManyToMany(targetEntity="CompanyFlexContract", mappedBy="managers", fetch="EXTRA_LAZY") + */ + public $managedContracts; + public function getTitle() { return $this->title; } @@ -34,4 +39,4 @@ class CompanyManager extends CompanyEmployee public function setCar(CompanyCar $car) { $this->car = $car; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 98b3fafd4..6444877d0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -34,6 +34,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase { private $userId, $userId2, $articleId, $articleId2; private $groupId, $groupId2; + private $managerId, $contractId1, $contractId2; public function setUp() { @@ -655,12 +656,89 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $contract4 = new CompanyFlexContract; $contract4->markCompleted(); + $manager = new CompanyManager; + $manager->setName('Alexander'); + $manager->setSalary(42); + $manager->setDepartment('Doctrine'); + $manager->setTitle('Filterer'); + + $contract1->addManager($manager); + $contract2->addManager($manager); + $contract3->addManager($manager); + $contract4->addManager($manager); + + $this->_em->persist($manager); $this->_em->persist($contract1); $this->_em->persist($contract2); $this->_em->persist($contract3); $this->_em->persist($contract4); $this->_em->flush(); $this->_em->clear(); + + $this->managerId = $manager->getId(); + $this->contractId1 = $contract1->getId(); + $this->contractId2 = $contract2->getId(); + } + + private function useCompletedContractFilter() + { + $conf = $this->_em->getConfiguration(); + $conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter"); + $this->_em->getFilters() + ->enable("completed_contract") + ->setParameter("completed", true, DBALType::BOOLEAN); + } + + public function testManyToMany_ExtraLazyCountWithFilterOnSTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + + $this->assertFalse($manager->managedContracts->isInitialized()); + $this->assertEquals(4, count($manager->managedContracts)); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->managedContracts->isInitialized()); + $this->assertEquals(2, count($manager->managedContracts)); + } + + public function testManyToMany_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->managedContracts->isInitialized()); + $this->assertTrue($manager->managedContracts->contains($contract1)); + $this->assertTrue($manager->managedContracts->contains($contract2)); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->managedContracts->isInitialized()); + $this->assertFalse($manager->managedContracts->contains($contract1)); + $this->assertTrue($manager->managedContracts->contains($contract2)); + } + + public function testManyToMany_ExtraLazySliceWithFilterOnSTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + + $this->assertFalse($manager->managedContracts->isInitialized()); + $this->assertEquals(4, count($manager->managedContracts->slice(0, 10))); + + // Enable the filter + $this->useCompletedContractFilter(); + + $this->assertFalse($manager->managedContracts->isInitialized()); + $this->assertEquals(2, count($manager->managedContracts->slice(0, 10))); } } From 223c47069eb0f8cfc3d0f7c6cc96780295def8a6 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Dec 2011 00:19:35 +0100 Subject: [PATCH 3/9] Added tests for ManyToMany associations and lazy collection to CTI entity --- .../Models/Company/CompanyFlexContract.php | 4 +- .../Tests/ORM/Functional/SQLFilterTest.php | 86 ++++++++++++++++--- .../Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php b/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php index 976506d7c..e32288897 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php @@ -19,13 +19,13 @@ class CompanyFlexContract extends CompanyContract private $pricePerHour = 0; /** - * @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts") + * @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts", fetch="EXTRA_LAZY") * @JoinTable(name="company_contract_managers", * joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")}, * inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")} * ) */ - private $managers; + public $managers; public function calculatePrice() { diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 6444877d0..403bf3a04 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -34,7 +34,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase { private $userId, $userId2, $articleId, $articleId2; private $groupId, $groupId2; - private $managerId, $contractId1, $contractId2; + private $managerId, $managerId2, $contractId1, $contractId2; public function setUp() { @@ -553,11 +553,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(2, count($this->_em->createQuery("SELECT cm FROM Doctrine\Tests\Models\Company\CompanyManager cm")->getResult())); // 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%", DBALType::STRING); + $this->usePersonNameFilter('Guilh%'); $managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll(); $this->assertEquals(1, count($managers)); @@ -573,11 +569,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(3, count($this->_em->createQuery("SELECT cp FROM Doctrine\Tests\Models\Company\CompanyPerson cp")->getResult())); // 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%", DBALType::STRING); + $this->usePersonNameFilter('Guilh%'); $persons = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll(); $this->assertEquals(1, count($persons)); @@ -662,12 +654,21 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $manager->setDepartment('Doctrine'); $manager->setTitle('Filterer'); + $manager2 = new CompanyManager; + $manager2->setName('Benjamin'); + $manager2->setSalary(1337); + $manager2->setDepartment('Doctrine'); + $manager2->setTitle('Maintainer'); + $contract1->addManager($manager); $contract2->addManager($manager); $contract3->addManager($manager); $contract4->addManager($manager); + $contract1->addManager($manager2); + $this->_em->persist($manager); + $this->_em->persist($manager2); $this->_em->persist($contract1); $this->_em->persist($contract2); $this->_em->persist($contract3); @@ -676,6 +677,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); $this->managerId = $manager->getId(); + $this->managerId2 = $manager2->getId(); $this->contractId1 = $contract1->getId(); $this->contractId2 = $contract2->getId(); } @@ -740,6 +742,68 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertFalse($manager->managedContracts->isInitialized()); $this->assertEquals(2, count($manager->managedContracts->slice(0, 10))); } + + private function usePersonNameFilter($name) + { + // Enable the filter + $conf = $this->_em->getConfiguration(); + $conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter"); + $this->_em->getFilters() + ->enable("person_name") + ->setParameter("name", $name, DBALType::STRING); + } + + public function testManyToMany_ExtraLazyCountWithFilterOnCTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertEquals(2, count($contract->managers)); + + // Enable the filter + $this->usePersonNameFilter('Benjamin'); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertEquals(1, count($contract->managers)); + } + + public function testManyToMany_ExtraLazyContainsWithFilterOnCTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1); + $manager1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId); + $manager2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId2); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertTrue($contract->managers->contains($manager1)); + $this->assertTrue($contract->managers->contains($manager2)); + + // Enable the filter + $this->usePersonNameFilter('Benjamin'); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertFalse($contract->managers->contains($manager1)); + $this->assertTrue($contract->managers->contains($manager2)); + } + + public function testManyToMany_ExtraLazySliceWithFilterOnCTI() + { + $this->loadCompanySingleTableInheritanceFixtureData(); + + $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertEquals(2, count($contract->managers->slice(0, 10))); + + // Enable the filter + $this->usePersonNameFilter('Benjamin'); + + $this->assertFalse($contract->managers->isInitialized()); + $this->assertEquals(1, count($contract->managers->slice(0, 10))); + } } class MySoftDeleteFilter extends SQLFilter diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index ba54da95c..2d405e785 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -158,6 +158,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase if (isset($this->_usedModelSets['company'])) { $conn->executeUpdate('DELETE FROM company_contract_employees'); + $conn->executeUpdate('DELETE FROM company_contract_managers'); $conn->executeUpdate('DELETE FROM company_contracts'); $conn->executeUpdate('DELETE FROM company_persons_friends'); $conn->executeUpdate('DELETE FROM company_managers'); From f49a4e9c40fbf3d0e7e3f13a078987ce490eef5a Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Dec 2011 20:50:57 +0100 Subject: [PATCH 4/9] Added tests for OneToMany associations and lazy collection to STI entity --- .../ORM/Persisters/OneToManyPersister.php | 3 +- .../Tests/Models/Company/CompanyContract.php | 2 +- .../Tests/Models/Company/CompanyEmployee.php | 5 ++ .../Tests/ORM/Functional/SQLFilterTest.php | 56 +++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) 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 From c3c174512a6732c86bf2bb85af9293a6eb8c5bc2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Dec 2011 21:10:13 +0100 Subject: [PATCH 5/9] Added tests for OneToMany associations and lazy collection to CTI entity --- .../Tests/Models/Company/CompanyAuction.php | 2 +- .../Models/Company/CompanyOrganization.php | 6 +- .../Tests/ORM/Functional/SQLFilterTest.php | 100 ++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/Models/Company/CompanyAuction.php b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php index 5dc72e8c1..5743122bc 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyAuction.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php @@ -14,4 +14,4 @@ class CompanyAuction extends CompanyEvent { public function getData() { return $this->data; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php index 19463206b..ca9941062 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php @@ -11,9 +11,9 @@ class CompanyOrganization { private $id; /** - * @OneToMany(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}) + * @OneToMany(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}, fetch="EXTRA_LAZY") */ - private $events; + public $events; public function getId() { return $this->id; @@ -41,4 +41,4 @@ class CompanyOrganization { public function setMainEvent($event) { $this->mainevent = $event; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 7d3affd0e..31ebb2d46 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -19,6 +19,8 @@ use Doctrine\Tests\Models\CMS\CmsComment; use Doctrine\Tests\Models\Company\CompanyPerson; use Doctrine\Tests\Models\Company\CompanyManager; use Doctrine\Tests\Models\Company\CompanyEmployee; +use Doctrine\Tests\Models\Company\CompanyOrganization; +use Doctrine\Tests\Models\Company\CompanyAuction; use Doctrine\Tests\Models\Company\CompanyFlexContract; use Doctrine\Tests\Models\Company\CompanyFlexUltraContract; @@ -35,6 +37,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase private $userId, $userId2, $articleId, $articleId2; private $groupId, $groupId2; private $managerId, $managerId2, $contractId1, $contractId2; + private $organizationId, $eventId1, $eventId2; public function setUp() { @@ -860,6 +863,91 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertFalse($manager->soldContracts->isInitialized()); $this->assertEquals(1, count($manager->soldContracts->slice(0, 10))); } + private function loadCompanyOrganizationEventJoinedSubclassFixtureData() + { + $organization = new CompanyOrganization; + + $event1 = new CompanyAuction; + $event1->setData('foo'); + + $event2 = new CompanyAuction; + $event2->setData('bar'); + + $organization->addEvent($event1); + $organization->addEvent($event2); + + $this->_em->persist($organization); + $this->_em->flush(); + $this->_em->clear(); + + $this->organizationId = $organization->getId(); + $this->eventId1 = $event1->getId(); + $this->eventId2 = $event2->getId(); + } + + private function useCompanyEventIdFilter() + { + // Enable the filter + $conf = $this->_em->getConfiguration(); + $conf->addFilter("event_id", "\Doctrine\Tests\ORM\Functional\CompanyEventFilter"); + $this->_em->getFilters() + ->enable("event_id") + ->setParameter("id", $this->eventId2); + } + + + public function testOneToMany_ExtraLazyCountWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(2, count($organization->events)); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(1, count($organization->events)); + } + + public function testOneToMany_ExtraLazyContainsWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $event1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyEvent', $this->eventId1); + $event2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyEvent', $this->eventId2); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertTrue($organization->events->contains($event1)); + $this->assertTrue($organization->events->contains($event2)); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertFalse($organization->events->contains($event1)); + $this->assertTrue($organization->events->contains($event2)); + } + + public function testOneToMany_ExtraLazySliceWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(2, count($organization->events->slice(0, 10))); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(1, count($organization->events->slice(0, 10))); + } } class MySoftDeleteFilter extends SQLFilter @@ -945,3 +1033,15 @@ class CompletedContractFilter extends SQLFilter return $targetTableAlias.'.completed = ' . $this->getParameter('completed'); } } + +class CompanyEventFilter extends SQLFilter +{ + public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias, $targetTable = '') + { + if ($targetEntity->name != "Doctrine\Tests\Models\Company\CompanyEvent") { + return ""; + } + + return $targetTableAlias.'.id = ' . $this->getParameter('id'); + } +} From 4cf5f70bea4c0be20df75a4a74286faadbaba33a Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Dec 2011 23:21:56 +0100 Subject: [PATCH 6/9] Update test --- .../Tests/ORM/Functional/SchemaTool/CompanySchemaTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/CompanySchemaTest.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/CompanySchemaTest.php index 797c202f6..a1e6daca5 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/CompanySchemaTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/CompanySchemaTest.php @@ -63,6 +63,6 @@ class CompanySchemaTest extends \Doctrine\Tests\OrmFunctionalTestCase $sql = $this->_schemaTool->getDropSchemaSQL(array( $this->_em->getClassMetadata('Doctrine\Tests\Models\Company\CompanyManager'), )); - $this->assertEquals(3, count($sql)); + $this->assertEquals(4, count($sql)); } -} \ No newline at end of file +} From bd49aa5d2c23be6e5d5f829bb9dce58deb28130c Mon Sep 17 00:00:00 2001 From: Adrien BRAULT Date: Fri, 23 Dec 2011 17:05:08 +0100 Subject: [PATCH 7/9] Fix some PHPDoc @return type. --- lib/Doctrine/ORM/Query/Expr.php | 4 ++-- lib/Doctrine/ORM/QueryBuilder.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Expr.php b/lib/Doctrine/ORM/Query/Expr.php index dce4c6044..8967608e2 100644 --- a/lib/Doctrine/ORM/Query/Expr.php +++ b/lib/Doctrine/ORM/Query/Expr.php @@ -75,7 +75,7 @@ class Expr * Creates an ASCending order expression. * * @param $sort - * @return OrderBy + * @return Expr\OrderBy */ public function asc($expr) { @@ -86,7 +86,7 @@ class Expr * Creates a DESCending order expression. * * @param $sort - * @return OrderBy + * @return Expr\OrderBy */ public function desc($expr) { diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 21143214b..c67506d98 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -119,7 +119,7 @@ class QueryBuilder * For more complex expression construction, consider storing the expression * builder object in a local variable. * - * @return Expr + * @return Query\Expr */ public function expr() { From 21cfe4ba9f88dacc346b4060d78d84bafe0f29d2 Mon Sep 17 00:00:00 2001 From: holtkamp Date: Thu, 22 Dec 2011 09:38:55 -0200 Subject: [PATCH 8/9] Allow ExporterDrivers that implement the exportClassMetadata() function to return FALSE when no content is available/needs to be written to a file by the AbstractExporter, preventing empty files to be generated foreach processed ClassMetadataInfo instance. --- .../Tools/Export/Driver/AbstractExporter.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php index 14f654130..41ec6fdf6 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php @@ -111,16 +111,18 @@ abstract class AbstractExporter } foreach ($this->_metadata as $metadata) { - $output = $this->exportClassMetadata($metadata); - $path = $this->_generateOutputPath($metadata); - $dir = dirname($path); - if ( ! is_dir($dir)) { - mkdir($dir, 0777, true); + //In case output is returned, write it to a file, skip otherwise + if($output = $this->exportClassMetadata($metadata)){ + $path = $this->_generateOutputPath($metadata); + $dir = dirname($path); + if ( ! is_dir($dir)) { + mkdir($dir, 0777, true); + } + if (file_exists($path) && !$this->_overwriteExistingFiles) { + throw ExportException::attemptOverwriteExistingFile($path); + } + file_put_contents($path, $output); } - if (file_exists($path) && !$this->_overwriteExistingFiles) { - throw ExportException::attemptOverwriteExistingFile($path); - } - file_put_contents($path, $output); } } From a6deb51a0547efec163d9b57a1571af54accb2f1 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 28 Dec 2011 20:28:17 +0100 Subject: [PATCH 9/9] DDC-1360 - Bugfix in quoting mechanism inside ClassMetadataInfo --- .../ORM/Mapping/ClassMetadataInfo.php | 2 +- .../ORM/Functional/Ticket/DDC1360Test.php | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1360Test.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 6fbf8b3c6..56dd16385 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1378,7 +1378,7 @@ class ClassMetadataInfo { if (isset($table['name'])) { if ($table['name'][0] == '`') { - $this->table['name'] = trim($table['name'], '`'); + $this->table['name'] = str_replace("`", "", $table['name']); $this->table['quoted'] = true; } else { $this->table['name'] = $table['name']; diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1360Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1360Test.php new file mode 100644 index 000000000..f95f77eb4 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1360Test.php @@ -0,0 +1,37 @@ +_em->getConnection()->getDatabasePlatform()->getName() != "postgresql") { + $this->markTestSkipped("PostgreSQL only test."); + } + + $sql = $this->_schemaTool->getCreateSchemaSQL(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1360DoubleQuote') + )); + + $this->assertEquals(array( + 'CREATE TABLE "user"."user" (id INT NOT NULL, PRIMARY KEY(id))', + 'CREATE SEQUENCE "user".user_id_seq INCREMENT BY 1 MINVALUE 1 START 1', + ), $sql); + } +} + +/** + * @Entity @Table(name="`user`.`user`") + */ +class DDC1360DoubleQuote +{ + /** @Id @GeneratedValue @Column(type="integer") */ + public $id; +} +