From af2295b73aae83aca5047403cdb6c3e7f4bc32c2 Mon Sep 17 00:00:00 2001 From: Wojciech Zylinski Date: Sun, 3 May 2015 18:39:51 +0100 Subject: [PATCH 01/14] Switch to relationToTargetKeyColumns when matching non-owning side with Criteria. Fixes DDC-3719. --- .../Collection/ManyToManyPersister.php | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index bf9f14e688..6b5dd554e0 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -229,29 +229,41 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri $mapping = $collection->getMapping(); $owner = $collection->getOwner(); $ownerMetadata = $this->em->getClassMetadata(get_class($owner)); + $id = $this->uow->getEntityIdentifier($owner); + $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); + $onConditions = $this->getOnConditionSQL($mapping); $whereClauses = $params = array(); - foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) { + if ( ! $mapping['isOwningSide']) { + $associationSourceClass = $targetClass; + $mapping = $targetClass->associationMappings[$mapping['mappedBy']]; + $sourceRelationMode = 'relationToTargetKeyColumns'; + } else { + $associationSourceClass = $ownerMetadata; + $sourceRelationMode = 'relationToSourceKeyColumns'; + } + + foreach ($mapping[$sourceRelationMode] as $key => $value) { $whereClauses[] = sprintf('t.%s = ?', $key); - $params[] = $ownerMetadata->getFieldValue($owner, $value); + $params[] = $ownerMetadata->containsForeignIdentifier + ? $id[$ownerMetadata->getFieldForColumn($value)] + : $id[$ownerMetadata->fieldNames[$value]]; } $parameters = $this->expandCriteriaParameters($criteria); foreach ($parameters as $parameter) { list($name, $value) = $parameter; - $whereClauses[] = sprintf('te.%s = ?', $name); + $field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform); + $whereClauses[] = sprintf('te.%s = ?', $field); $params[] = $value; } - $mapping = $collection->getMapping(); - $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); $tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform); - $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform); - $onConditions = $this->getOnConditionSQL($mapping); + $joinTable = $this->quoteStrategy->getJoinTableName($mapping, $associationSourceClass, $this->platform); $rsm = new Query\ResultSetMappingBuilder($this->em); - $rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te'); + $rsm->addRootEntityFromClassMetadata($targetClass->name, 'te'); $sql = 'SELECT ' . $rsm->generateSelectClause() . ' FROM ' . $tableName . ' te' From 375b0369ae7b3343fb62bb5558584a2b447fee04 Mon Sep 17 00:00:00 2001 From: Jean Carlo Machado Date: Thu, 28 Jan 2016 19:36:52 -0200 Subject: [PATCH 02/14] added test of matching non-owing side with criteria --- .../ORM/Functional/Ticket/DDC3719Test.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php new file mode 100644 index 0000000000..28e65b32eb --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php @@ -0,0 +1,54 @@ + + */ +class DDC3719Test extends \Doctrine\Tests\OrmFunctionalTestCase +{ + public function setUp() + { + $this->useModelSet('company'); + parent::setUp(); + } + + /** + * @group DDC-3719 + */ + public function testCriteriaOnNotOwningSide() + { + $manager = new CompanyManager(); + $manager->setName('Gandalf'); + $manager->setSalary(666); + $manager->setTitle('Boss'); + $manager->setDepartment('Marketing'); + $this->_em->persist($manager); + + $contractA = new CompanyFlexContract(); + $contractA->markCompleted(); + $contractA->addManager($manager); + $this->_em->persist($contractA); + + $contractB = new CompanyFlexContract(); + $contractB->addManager($manager); + $this->_em->persist($contractB); + + $this->_em->flush(); + $this->_em->refresh($manager); + + $contracts = $manager->managedContracts; + static::assertCount(2, $contracts); + + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq("completed", true)); + + $completedContracts = $contracts->matching($criteria); + static::assertCount(1, $completedContracts); + } +} From 468fe315bae21999ff4d85022f70c091d141a0a5 Mon Sep 17 00:00:00 2001 From: Jean Carlo Machado Date: Fri, 29 Jan 2016 10:03:44 -0200 Subject: [PATCH 03/14] fixed indentation --- .../ORM/Functional/Ticket/DDC3719Test.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php index 28e65b32eb..f5f60c0b0d 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php @@ -24,10 +24,10 @@ public function setUp() public function testCriteriaOnNotOwningSide() { $manager = new CompanyManager(); - $manager->setName('Gandalf'); - $manager->setSalary(666); - $manager->setTitle('Boss'); - $manager->setDepartment('Marketing'); + $manager->setName('Gandalf'); + $manager->setSalary(666); + $manager->setTitle('Boss'); + $manager->setDepartment('Marketing'); $this->_em->persist($manager); $contractA = new CompanyFlexContract(); @@ -43,12 +43,12 @@ public function testCriteriaOnNotOwningSide() $this->_em->refresh($manager); $contracts = $manager->managedContracts; - static::assertCount(2, $contracts); + static::assertCount(2, $contracts); - $criteria = Criteria::create(); - $criteria->where(Criteria::expr()->eq("completed", true)); + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq("completed", true)); - $completedContracts = $contracts->matching($criteria); - static::assertCount(1, $completedContracts); + $completedContracts = $contracts->matching($criteria); + static::assertCount(1, $completedContracts); } } From cfeda903e37c21eaeaaae3a292d64814dcdcdd11 Mon Sep 17 00:00:00 2001 From: Patrick Poulain Date: Fri, 5 Feb 2016 11:14:47 +0100 Subject: [PATCH 04/14] Fix many-to-many matching ignoring offset/limit --- .../ORM/Persisters/Collection/ManyToManyPersister.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index 63d996db5a..5a506b5149 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -266,6 +266,13 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses); + $limit = $criteria->getMaxResults(); + $offset = $criteria->getFirstResult(); + if ($limit !== null || $offset !== null) { + $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset); + } + + $stmt = $this->conn->executeQuery($sql, $params); return $this From 0adeade045795524ee553f8d1f0399e8f5c62a91 Mon Sep 17 00:00:00 2001 From: Jeremy Giberson Date: Wed, 10 Feb 2016 19:52:03 -0700 Subject: [PATCH 05/14] merged git@github.com:SammyK/doctrine2.git:many-to-many-order-by-fix --- .../Collection/ManyToManyPersister.php | 20 ++++++++++ .../ManyToManyBasicAssociationTest.php | 38 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index f476f2d0c6..eeebc7b8a2 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -278,6 +278,8 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses); + $sql .= $this->getOrderingSql($criteria); + $stmt = $this->conn->executeQuery($sql, $params); return $this @@ -740,4 +742,22 @@ private function expandCriteriaParameters(Criteria $criteria) return $types; } + + /** + * @param Criteria $criteria + * @return string + */ + private function getOrderingSql(Criteria $criteria) + { + $orderings = $criteria->getOrderings(); + if ($orderings) { + $orderBy = []; + foreach ($orderings as $field => $direction) { + $orderBy[] = $field . ' ' . $direction; + } + + return ' ORDER BY ' . implode(', ', $orderBy); + } + return ''; + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index e6e42f0a0d..9a020a8aad 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -377,6 +377,44 @@ public function testClearBeforeLazyLoad() $this->assertEquals(0, count($user->groups)); } + /** + * @group DDC-3952 + */ + public function testManyToManyOrderByIsNotIgnored() + { + $user = $this->addCmsUserGblancoWithGroups(1); + + $group = new CmsGroup; + $group->name = 'C'; + $user->addGroup($group); + + $group = new CmsGroup; + $group->name = 'A'; + $user->addGroup($group); + + $group = new CmsGroup; + $group->name = 'B'; + $user->addGroup($group); + + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->clear(); + + $user = $this->_em->find(get_class($user), $user->id); + + $criteria = Criteria::create() + ->orderBy(['name' => Criteria::ASC]); + $groups = $user->getGroups()->matching($criteria); + + $existingOrder = []; + foreach ($groups as $group) { + $existingOrder[] = $group->getName(); + } + + $this->assertEquals(['A', 'B', 'C', 'Developers_0'], $existingOrder); + } + public function testMatching() { $user = $this->addCmsUserGblancoWithGroups(2); From 0feaf92348e33f2726e2a7e95d474ac4361cb0cc Mon Sep 17 00:00:00 2001 From: Jeremy Giberson Date: Wed, 10 Feb 2016 20:27:43 -0700 Subject: [PATCH 06/14] improve test readability --- .../ManyToManyBasicAssociationTest.php | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index 8706cdacee..b4c10620d4 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -384,17 +384,17 @@ public function testManyToManyOrderByIsNotIgnored() { $user = $this->addCmsUserGblancoWithGroups(1); - $group = new CmsGroup; - $group->name = 'C'; - $user->addGroup($group); + $group1 = new CmsGroup; + $group2 = new CmsGroup; + $group3 = new CmsGroup; - $group = new CmsGroup; - $group->name = 'A'; - $user->addGroup($group); + $group1->name = 'C'; + $group2->name = 'A'; + $group3->name = 'B'; - $group = new CmsGroup; - $group->name = 'B'; - $user->addGroup($group); + $user->addGroup($group1); + $user->addGroup($group2); + $user->addGroup($group3); $this->_em->persist($user); $this->_em->flush(); @@ -405,14 +405,17 @@ public function testManyToManyOrderByIsNotIgnored() $criteria = Criteria::create() ->orderBy(['name' => Criteria::ASC]); - $groups = $user->getGroups()->matching($criteria); - $existingOrder = []; - foreach ($groups as $group) { - $existingOrder[] = $group->getName(); - } - - $this->assertEquals(['A', 'B', 'C', 'Developers_0'], $existingOrder); + $this->assertEquals( + ['A', 'B', 'C', 'Developers_0'], + $user + ->getGroups() + ->matching($criteria) + ->map(function (CmsGroup $group) { + return $group->getName(); + }) + ->toArray() + ); } public function testMatchingWithLimit() From dd3f67d86271d62566d2ee27b7a2fe2c540e347d Mon Sep 17 00:00:00 2001 From: Jeremy Giberson Date: Thu, 11 Feb 2016 13:39:31 -0700 Subject: [PATCH 07/14] updated manytomany so it maps field names to column names in criteria ordering --- .../Collection/ManyToManyPersister.php | 12 +++-- tests/Doctrine/Tests/Models/CMS/CmsTag.php | 48 +++++++++++++++++++ tests/Doctrine/Tests/Models/CMS/CmsUser.php | 18 +++++++ .../ManyToManyBasicAssociationTest.php | 45 +++++++++++++++++ .../Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/CMS/CmsTag.php diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index cb620210be..25c03a4505 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -278,7 +278,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses); - $sql .= $this->getOrderingSql($criteria); + $sql .= $this->getOrderingSql($criteria, $targetClass); $sql .= $this->getLimitSql($criteria); @@ -747,14 +747,20 @@ private function expandCriteriaParameters(Criteria $criteria) /** * @param Criteria $criteria + * @param ClassMetadata $targetClass * @return string */ - private function getOrderingSql(Criteria $criteria) + private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass) { $orderings = $criteria->getOrderings(); if ($orderings) { $orderBy = []; - foreach ($orderings as $field => $direction) { + foreach ($orderings as $name => $direction) { + $field = $this->quoteStrategy->getColumnName( + $name, + $targetClass, + $this->platform + ); $orderBy[] = $field . ' ' . $direction; } diff --git a/tests/Doctrine/Tests/Models/CMS/CmsTag.php b/tests/Doctrine/Tests/Models/CMS/CmsTag.php new file mode 100644 index 0000000000..cf7486cb08 --- /dev/null +++ b/tests/Doctrine/Tests/Models/CMS/CmsTag.php @@ -0,0 +1,48 @@ +name = $name; + } + + public function getName() { + return $this->name; + } + + public function addUser(CmsUser $user) { + $this->users[] = $user; + } + + public function getUsers() { + return $this->users; + } +} + diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index c95cce4ea4..468c712205 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -162,6 +162,14 @@ class CmsUser * ) */ public $groups; + /** + * @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"persist", "merge", "detach"}) + * @JoinTable(name="cms_users_tags", + * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, + * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")} + * ) + */ + public $tags; public $nonPersistedProperty; @@ -171,6 +179,7 @@ public function __construct() { $this->phonenumbers = new ArrayCollection; $this->articles = new ArrayCollection; $this->groups = new ArrayCollection; + $this->tags = new ArrayCollection; } public function getId() { @@ -217,6 +226,15 @@ public function getGroups() { return $this->groups; } + public function addTag(CmsTag $tag) { + $this->tags[] = $tag; + $tag->addUser($this); + } + + public function getTags() { + return $this->tags; + } + public function removePhonenumber($index) { if (isset($this->phonenumbers[$index])) { $ph = $this->phonenumbers[$index]; diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index b4c10620d4..da4d300773 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Common\Collections\Criteria; +use Doctrine\Tests\Models\CMS\CmsTag; use Doctrine\Tests\Models\CMS\CmsUser, Doctrine\Tests\Models\CMS\CmsGroup, Doctrine\Common\Collections\ArrayCollection; @@ -418,6 +419,50 @@ public function testManyToManyOrderByIsNotIgnored() ); } + /** + * @group DDC-3952 + */ + public function testManyToManyOrderByHonorsFieldNameColumnNameAliases() + { + $user = new CmsUser; + $user->name = 'Guilherme'; + $user->username = 'gblanco'; + $user->status = 'developer'; + + $tag1 = new CmsTag; + $tag2 = new CmsTag; + $tag3 = new CmsTag; + + $tag1->name = 'C'; + $tag2->name = 'A'; + $tag3->name = 'B'; + + $user->addTag($tag1); + $user->addTag($tag2); + $user->addTag($tag3); + + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->clear(); + + $user = $this->_em->find(get_class($user), $user->id); + + $criteria = Criteria::create() + ->orderBy(['name' => Criteria::ASC]); + + $this->assertEquals( + ['A', 'B', 'C'], + $user + ->getTags() + ->matching($criteria) + ->map(function (CmsTag $tag) { + return $tag->getName(); + }) + ->toArray() + ); + } + public function testMatchingWithLimit() { $user = $this->addCmsUserGblancoWithGroups(2); diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index b8a705707a..f19e19ff99 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -83,6 +83,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\CMS\CmsAddress', 'Doctrine\Tests\Models\CMS\CmsEmail', 'Doctrine\Tests\Models\CMS\CmsGroup', + 'Doctrine\Tests\Models\CMS\CmsTag', 'Doctrine\Tests\Models\CMS\CmsArticle', 'Doctrine\Tests\Models\CMS\CmsComment', ), From 3ed64dcec2183cdf0af27489b9f86f8af6035aa4 Mon Sep 17 00:00:00 2001 From: "jeremygiberson@gmail.com" Date: Sun, 14 Feb 2016 17:37:25 -0700 Subject: [PATCH 08/14] changed relation table column name to match --- tests/Doctrine/Tests/Models/CMS/CmsUser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index 468c712205..da4b102702 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -166,7 +166,7 @@ class CmsUser * @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"persist", "merge", "detach"}) * @JoinTable(name="cms_users_tags", * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, - * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")} + * inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")} * ) */ public $tags; From 1e660abeb8ff8295b52f6efa6462c03183db377c Mon Sep 17 00:00:00 2001 From: "jeremygiberson@gmail.com" Date: Sun, 14 Feb 2016 17:43:58 -0700 Subject: [PATCH 09/14] cascade all operations on cmstags --- tests/Doctrine/Tests/Models/CMS/CmsUser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index da4b102702..10042efb52 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -163,7 +163,7 @@ class CmsUser */ public $groups; /** - * @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"persist", "merge", "detach"}) + * @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"all"}) * @JoinTable(name="cms_users_tags", * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")} From cc7ef71a138eda7c570c6a9e6bfaf5730b845ca1 Mon Sep 17 00:00:00 2001 From: "jeremygiberson@gmail.com" Date: Sun, 14 Feb 2016 17:48:53 -0700 Subject: [PATCH 10/14] added teardown for new cmstag table --- tests/Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index f19e19ff99..9a6cd84142 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -320,6 +320,7 @@ protected function tearDown() if (isset($this->_usedModelSets['cms'])) { $conn->executeUpdate('DELETE FROM cms_users_groups'); $conn->executeUpdate('DELETE FROM cms_groups'); + $conn->executeUpdate('DELETE FROM cms_tags'); $conn->executeUpdate('DELETE FROM cms_addresses'); $conn->executeUpdate('DELETE FROM cms_phonenumbers'); $conn->executeUpdate('DELETE FROM cms_comments'); From dd64161ece6fd5e37f13de23d15ef49b6b6f38e9 Mon Sep 17 00:00:00 2001 From: "jeremygiberson@gmail.com" Date: Sun, 14 Feb 2016 18:00:15 -0700 Subject: [PATCH 11/14] forgot to delete from relational table first --- tests/Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 9a6cd84142..d40b310827 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -320,6 +320,7 @@ protected function tearDown() if (isset($this->_usedModelSets['cms'])) { $conn->executeUpdate('DELETE FROM cms_users_groups'); $conn->executeUpdate('DELETE FROM cms_groups'); + $conn->executeUpdate('DELETE FROM cms_users_tags'); $conn->executeUpdate('DELETE FROM cms_tags'); $conn->executeUpdate('DELETE FROM cms_addresses'); $conn->executeUpdate('DELETE FROM cms_phonenumbers'); From cbde629bf083646ab198931a79f1a68dcd314156 Mon Sep 17 00:00:00 2001 From: Patrick Poulain Date: Mon, 15 Feb 2016 17:41:18 +0100 Subject: [PATCH 12/14] Fix MySql Tests --- .../SchemaTool/MySqlSchemaToolTest.php | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php index 244d44ed60..4df6032d0e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php @@ -19,6 +19,7 @@ public function testGetCreateSchemaSql() $classes = array( $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'), $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsTag'), $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'), $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmail'), $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'), @@ -30,16 +31,20 @@ public function testGetCreateSchemaSql() $this->assertEquals("CREATE TABLE cms_groups (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[0]); $this->assertEquals("CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, email_id INT DEFAULT NULL, status VARCHAR(50) DEFAULT NULL, username VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_3AF03EC5F85E0677 (username), UNIQUE INDEX UNIQ_3AF03EC5A832C1C9 (email_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[1]); $this->assertEquals("CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, INDEX IDX_7EA9409AA76ED395 (user_id), INDEX IDX_7EA9409AFE54D947 (group_id), PRIMARY KEY(user_id, group_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[2]); - $this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, UNIQUE INDEX UNIQ_ACAC157BA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[3]); - $this->assertEquals("CREATE TABLE cms_emails (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(250) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[4]); - $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, INDEX IDX_F21F790FA76ED395 (user_id), PRIMARY KEY(phonenumber)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[5]); - $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id)", $sql[6]); - $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[7]); - $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id)", $sql[8]); - $this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[9]); - $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[10]); - - $this->assertEquals(11, count($sql)); + $this->assertEquals("CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, INDEX IDX_93F5A1ADA76ED395 (user_id), INDEX IDX_93F5A1ADBAD26311 (tag_id), PRIMARY KEY(user_id, tag_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[3]); + $this->assertEquals("CREATE TABLE cms_tags (id INT AUTO_INCREMENT NOT NULL, tag_name VARCHAR(50) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[4]); + $this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, UNIQUE INDEX UNIQ_ACAC157BA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[5]); + $this->assertEquals("CREATE TABLE cms_emails (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(250) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[6]); + $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, INDEX IDX_F21F790FA76ED395 (user_id), PRIMARY KEY(phonenumber)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB", $sql[7]); + $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id)", $sql[8]); + $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[9]); + $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id)", $sql[10]); + $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[11]); + $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id)", $sql[12]); + $this->assertEquals("ALTER TABLE cms_addresses ADD CONSTRAINT FK_ACAC157BA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[13]); + $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id)", $sql[14]); + + $this->assertEquals(15, count($sql)); } public function testGetCreateSchemaSql2() From e8296e8e7d9dbbb6fc13e815e62c62006c7643df Mon Sep 17 00:00:00 2001 From: Patrick Poulain Date: Mon, 15 Feb 2016 18:18:22 +0100 Subject: [PATCH 13/14] Fix ORM Driver Tests --- tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php index 64295f3cfe..29d7185601 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php @@ -117,15 +117,18 @@ public function testDetectManyToManyTables() $this->markTestSkipped('Platform does not support foreign keys.'); } - $metadatas = $this->extractClassMetadata(array("CmsUsers", "CmsGroups")); + $metadatas = $this->extractClassMetadata(array("CmsUsers", "CmsGroups", "CmsTags")); $this->assertArrayHasKey('CmsUsers', $metadatas, 'CmsUsers entity was not detected.'); $this->assertArrayHasKey('CmsGroups', $metadatas, 'CmsGroups entity was not detected.'); + $this->assertArrayHasKey('CmsTags', $metadatas, 'CmsTags entity was not detected.'); - $this->assertEquals(2, count($metadatas['CmsUsers']->associationMappings)); + $this->assertEquals(3, count($metadatas['CmsUsers']->associationMappings)); $this->assertArrayHasKey('group', $metadatas['CmsUsers']->associationMappings); $this->assertEquals(1, count($metadatas['CmsGroups']->associationMappings)); $this->assertArrayHasKey('user', $metadatas['CmsGroups']->associationMappings); + $this->assertEquals(1, count($metadatas['CmsTags']->associationMappings)); + $this->assertArrayHasKey('user', $metadatas['CmsGroups']->associationMappings); } public function testIgnoreManyToManyTableWithoutFurtherForeignKeyDetails() From 9461839d4240c67a6f6f67091aae179a85278797 Mon Sep 17 00:00:00 2001 From: Patrick Poulain Date: Mon, 15 Feb 2016 18:29:07 +0100 Subject: [PATCH 14/14] Fix PostgreSql Tests --- .../Functional/SchemaTool/PostgreSqlSchemaToolTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php index d6fa012b93..52151e4ba8 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php @@ -43,6 +43,9 @@ public function testGetCreateSchemaSql() $this->assertEquals("CREATE TABLE cms_users_groups (user_id INT NOT NULL, group_id INT NOT NULL, PRIMARY KEY(user_id, group_id))", array_shift($sql)); $this->assertEquals("CREATE INDEX IDX_7EA9409AA76ED395 ON cms_users_groups (user_id)", array_shift($sql)); $this->assertEquals("CREATE INDEX IDX_7EA9409AFE54D947 ON cms_users_groups (group_id)", array_shift($sql)); + $this->assertEquals("CREATE TABLE cms_users_tags (user_id INT NOT NULL, tag_id INT NOT NULL, PRIMARY KEY(user_id, tag_id))", array_shift($sql)); + $this->assertEquals("CREATE INDEX IDX_93F5A1ADA76ED395 ON cms_users_tags (user_id)", array_shift($sql)); + $this->assertEquals("CREATE INDEX IDX_93F5A1ADBAD26311 ON cms_users_tags (tag_id)", array_shift($sql)); $this->assertEquals("CREATE TABLE cms_phonenumbers (phonenumber VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(phonenumber))", array_shift($sql)); $this->assertEquals("CREATE INDEX IDX_F21F790FA76ED395 ON cms_phonenumbers (user_id)", array_shift($sql)); $this->assertEquals("CREATE SEQUENCE cms_addresses_id_seq INCREMENT BY 1 MINVALUE 1 START 1", array_shift($sql)); @@ -51,10 +54,12 @@ public function testGetCreateSchemaSql() $this->assertEquals("ALTER TABLE cms_users ADD CONSTRAINT FK_3AF03EC5A832C1C9 FOREIGN KEY (email_id) REFERENCES cms_emails (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); $this->assertEquals("ALTER TABLE cms_users_groups ADD CONSTRAINT FK_7EA9409AFE54D947 FOREIGN KEY (group_id) REFERENCES cms_groups (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); + $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); + $this->assertEquals("ALTER TABLE cms_users_tags ADD CONSTRAINT FK_93F5A1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES cms_tags (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); $this->assertEquals("ALTER TABLE cms_phonenumbers ADD CONSTRAINT FK_F21F790FA76ED395 FOREIGN KEY (user_id) REFERENCES cms_users (id) NOT DEFERRABLE INITIALLY IMMEDIATE", array_shift($sql)); $this->assertEquals(array(), $sql, "SQL Array should be empty now."); - $this->assertEquals(17, $sqlCount, "Total of 17 queries should be executed"); + $this->assertEquals(22, $sqlCount, "Total of 22 queries should be executed"); } public function testGetCreateSchemaSql2() @@ -97,7 +102,7 @@ public function testGetDropSchemaSql() $tool = new SchemaTool($this->_em); $sql = $tool->getDropSchemaSQL($classes); - $this->assertEquals(14, count($sql)); + $this->assertEquals(17, count($sql)); $dropSequenceSQLs = 0;