From c68edec0c243c9459e80b959748f68753b2b9da5 Mon Sep 17 00:00:00 2001 From: Lenard Palko Date: Fri, 17 Apr 2015 14:50:31 +0300 Subject: [PATCH 1/5] Fix skipping properties if they are listed after a not loaded relation. --- lib/Doctrine/ORM/UnitOfWork.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index fe12e55df..194e45e30 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -3378,7 +3378,7 @@ class UnitOfWork implements PropertyChangedListener } else { if ($other instanceof Proxy && !$other->__isInitialized()) { // do not merge fields marked lazy that have not been fetched. - return; + continue; } if ( ! $assoc2['isCascadeMerge']) { @@ -3406,7 +3406,7 @@ class UnitOfWork implements PropertyChangedListener if ($mergeCol instanceof PersistentCollection && ! $mergeCol->isInitialized()) { // do not merge fields marked lazy that have not been fetched. // keep the lazy persistent collection of the managed copy. - return; + continue; } $managedCol = $prop->getValue($managedCopy); From 69ef75ff2dfcf60117a6872af83da395602950bc Mon Sep 17 00:00:00 2001 From: Lenard Palko Date: Sun, 19 Apr 2015 16:43:28 +0300 Subject: [PATCH 2/5] Added test cases for both one-to-one and one-to-many cases. --- .../Tests/Models/DDC3699/DDC3699Child.php | 91 ++++++++++++++++++ .../Tests/Models/DDC3699/DDC3699Parent.php | 26 +++++ .../Models/DDC3699/DDC3699RelationMany.php | 44 +++++++++ .../Models/DDC3699/DDC3699RelationOne.php | 43 +++++++++ .../ORM/Functional/Ticket/DDC3699Test.php | 96 +++++++++++++++++++ 5 files changed, 300 insertions(+) create mode 100644 tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php create mode 100644 tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php create mode 100644 tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php create mode 100644 tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php new file mode 100644 index 000000000..d96649f37 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php @@ -0,0 +1,91 @@ +id; + } + + public function setId($id) + { + $this->id = $id; + } + + public function getChildField() + { + return $this->childField; + } + + public function setChildField($childField) + { + $this->childField = $childField; + } + + public function getOneRelation() + { + return $this->oneRelation; + } + + public function setOneRelation($oneRelation) + { + $this->oneRelation = $oneRelation; + } + + public function hasRelation($relation) + { + return $this->relations && $this->relations->contains($relation); + } + + public function addRelation($relation) + { + if (!$this->hasRelation($relation)) { + $this->relations[] = $relation; + } + + return $this; + } + + public function removeRelation($relation) + { + $this->relations->removeElement($relation); + + return $this; + } + + public function getRelations() + { + return $this->relations; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php new file mode 100644 index 000000000..aff175c02 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php @@ -0,0 +1,26 @@ +parentField; + } + + public function setParentField($parentField) + { + $this->parentField = $parentField; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php new file mode 100644 index 000000000..374ad92bb --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php @@ -0,0 +1,44 @@ +id; + } + + public function setId($id) + { + $this->id = $id; + } + + public function getChild() + { + return $this->child; + } + + public function setChild($child) + { + $this->child = $child; + } +} diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php new file mode 100644 index 000000000..f28a43abf --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php @@ -0,0 +1,43 @@ +id; + } + + public function setId($id) + { + $this->id = $id; + } + + public function getChild() + { + return $this->child; + } + + public function setChild($child) + { + $this->child = $child; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php new file mode 100644 index 000000000..b782980ee --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php @@ -0,0 +1,96 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(DDC3699Parent::CLASSNAME), + $this->_em->getClassMetadata(DDC3699RelationOne::CLASSNAME), + $this->_em->getClassMetadata(DDC3699RelationMany::CLASSNAME), + $this->_em->getClassMetadata(DDC3699Child::CLASSNAME), + )); + } catch (\Exception $e) { + // should throw error on second because schema is already created + } + } + + private function createChild($id, $relationClass, $relationMethod) + { + // element in DB + $child = new DDC3699Child(); + $child->setId($id); + $child->setChildField('childValue'); + $child->setParentField('parentValue'); + + $relation = new $relationClass(); + $relation->setId($id); + $relation->setChild($child); + $child->$relationMethod($relation); + + $this->_em->persist($relation); + $this->_em->persist($child); + $this->_em->flush(); + + // detach + $this->_em->detach($relation); + $this->_em->detach($child); + } + + /** + * @group DDC-3699 + */ + public function testMergeParentEntityFieldsOne() + { + $id = 1; + $this->createChild($id, DDC3699RelationOne::CLASSNAME, 'setOneRelation'); + + $unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); + $this->_em->detach($unmanagedChild); + + // make it managed again + $this->_em->find(DDC3699Child::CLASSNAME, $id); + + $unmanagedChild->setChildField('modifiedChildValue'); + $unmanagedChild->setParentField('modifiedParentValue'); + + $mergedChild = $this->_em->merge($unmanagedChild); + + $this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); + $this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); + } + + /** + * @group DDC-3699 + */ + public function testMergeParentEntityFieldsMany() + { + $id = 2; + $this->createChild($id, DDC3699RelationMany::CLASSNAME, 'addRelation'); + + $unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); + $this->_em->detach($unmanagedChild); + + // make it managed again + $this->_em->find(DDC3699Child::CLASSNAME, $id); + + $unmanagedChild->setChildField('modifiedChildValue'); + $unmanagedChild->setParentField('modifiedParentValue'); + + $mergedChild = $this->_em->merge($unmanagedChild); + + $this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); + $this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); + } +} \ No newline at end of file From 86abbb0e786c63a5b9ed997c983f963aa26362be Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 15 Jul 2015 21:46:23 +0100 Subject: [PATCH 3/5] DDC-3699 - #1387 - simpifying tests, clarifying on test method names --- .../Tests/Models/DDC3699/DDC3699Child.php | 88 ++----------------- .../Tests/Models/DDC3699/DDC3699Parent.php | 20 +---- .../Models/DDC3699/DDC3699RelationMany.php | 34 +------ .../Models/DDC3699/DDC3699RelationOne.php | 33 +------ .../ORM/Functional/Ticket/DDC3699Test.php | 86 ++++++++++-------- 5 files changed, 71 insertions(+), 190 deletions(-) diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php index d96649f37..320f73138 100644 --- a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php @@ -2,90 +2,20 @@ namespace Doctrine\Tests\Models\DDC3699; -use Doctrine\Common\Collections\ArrayCollection; - -/** - * @Entity - * @Table(name="ddc3699_child") - */ +/** @Entity @Table(name="ddc3699_child") */ class DDC3699Child extends DDC3699Parent { const CLASSNAME = __CLASS__; - /** - * @Id - * @Column(type="integer") - */ - protected $id; + /** @Id @Column(type="integer") */ + public $id; - /** - * @Column(type="string") - */ - protected $childField; + /** @Column(type="string") */ + public $childField; - /** - * @OneToOne(targetEntity="DDC3699RelationOne", inversedBy="child") - */ - protected $oneRelation; + /** @OneToOne(targetEntity="DDC3699RelationOne", inversedBy="child") */ + public $oneRelation; - /** - * @OneToMany(targetEntity="DDC3699RelationMany", mappedBy="child") - */ - protected $relations; - - public function getId() - { - return $this->id; - } - - public function setId($id) - { - $this->id = $id; - } - - public function getChildField() - { - return $this->childField; - } - - public function setChildField($childField) - { - $this->childField = $childField; - } - - public function getOneRelation() - { - return $this->oneRelation; - } - - public function setOneRelation($oneRelation) - { - $this->oneRelation = $oneRelation; - } - - public function hasRelation($relation) - { - return $this->relations && $this->relations->contains($relation); - } - - public function addRelation($relation) - { - if (!$this->hasRelation($relation)) { - $this->relations[] = $relation; - } - - return $this; - } - - public function removeRelation($relation) - { - $this->relations->removeElement($relation); - - return $this; - } - - public function getRelations() - { - return $this->relations; - } + /** @OneToMany(targetEntity="DDC3699RelationMany", mappedBy="child") */ + public $relations; } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php index aff175c02..09cfedaef 100644 --- a/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php @@ -2,25 +2,11 @@ namespace Doctrine\Tests\Models\DDC3699; -/** - * @MappedSuperclass - */ +/** @MappedSuperclass */ abstract class DDC3699Parent { const CLASSNAME = __CLASS__; - /** - * @Column(type="string") - */ - protected $parentField; - - public function getParentField() - { - return $this->parentField; - } - - public function setParentField($parentField) - { - $this->parentField = $parentField; - } + /** @Column(type="string") */ + public $parentField; } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php index 374ad92bb..3e0c06a67 100644 --- a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php @@ -10,35 +10,9 @@ class DDC3699RelationMany { const CLASSNAME = __CLASS__; - /** - * @Id - * @Column(type="integer") - */ - protected $id; + /** @Id @Column(type="integer") */ + public $id; - /** - * @ManyToOne(targetEntity="DDC3699Child", inversedBy="relations") - * @JoinColumn(name="child", referencedColumnName="id") - */ - protected $child; - - public function getId() - { - return $this->id; - } - - public function setId($id) - { - $this->id = $id; - } - - public function getChild() - { - return $this->child; - } - - public function setChild($child) - { - $this->child = $child; - } + /** @ManyToOne(targetEntity="DDC3699Child", inversedBy="relations") */ + public $child; } diff --git a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php index f28a43abf..c63558d35 100644 --- a/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php +++ b/tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php @@ -10,34 +10,9 @@ class DDC3699RelationOne { const CLASSNAME = __CLASS__; - /** - * @Id - * @Column(type="integer") - */ - protected $id; + /** @Id @Column(type="integer") */ + public $id; - /** - * @OneToOne(targetEntity="DDC3699Child", mappedBy="oneRelation") - */ - protected $child; - - public function getId() - { - return $this->id; - } - - public function setId($id) - { - $this->id = $id; - } - - public function getChild() - { - return $this->child; - } - - public function setChild($child) - { - $this->child = $child; - } + /** @OneToOne(targetEntity="DDC3699Child", mappedBy="oneRelation") */ + public $child; } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php index b782980ee..c3366ec81 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php @@ -26,71 +26,87 @@ class DDC3597Test extends \Doctrine\Tests\OrmFunctionalTestCase } } - private function createChild($id, $relationClass, $relationMethod) + /** + * @group DDC-3699 + */ + public function testMergingParentClassFieldsDoesNotStopMergingScalarFieldsForToOneUninitializedAssociations() { - // element in DB - $child = new DDC3699Child(); - $child->setId($id); - $child->setChildField('childValue'); - $child->setParentField('parentValue'); + $id = 1; - $relation = new $relationClass(); - $relation->setId($id); - $relation->setChild($child); - $child->$relationMethod($relation); + $child = new DDC3699Child(); + + $child->id = $id; + $child->childField = 'childValue'; + $child->parentField = 'parentValue'; + + $relation = new DDC3699RelationOne(); + + $relation->id = $id; + $relation->child = $child ; + $child->oneRelation = $relation; $this->_em->persist($relation); $this->_em->persist($child); $this->_em->flush(); + $this->_em->clear(); - // detach - $this->_em->detach($relation); - $this->_em->detach($child); - } + // fixtures loaded + /* @var $unManagedChild DDC3699Child */ + $unManagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); - /** - * @group DDC-3699 - */ - public function testMergeParentEntityFieldsOne() - { - $id = 1; - $this->createChild($id, DDC3699RelationOne::CLASSNAME, 'setOneRelation'); - - $unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); - $this->_em->detach($unmanagedChild); + $this->_em->detach($unManagedChild); // make it managed again $this->_em->find(DDC3699Child::CLASSNAME, $id); - $unmanagedChild->setChildField('modifiedChildValue'); - $unmanagedChild->setParentField('modifiedParentValue'); + $unManagedChild->childField = 'modifiedChildValue'; + $unManagedChild->parentField = 'modifiedParentValue'; - $mergedChild = $this->_em->merge($unmanagedChild); + /* @var $mergedChild DDC3699Child */ + $mergedChild = $this->_em->merge($unManagedChild); - $this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); - $this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); + $this->assertSame($mergedChild->childField, 'modifiedChildValue'); + $this->assertSame($mergedChild->parentField, 'modifiedParentValue'); } /** * @group DDC-3699 */ - public function testMergeParentEntityFieldsMany() + public function testMergingParentClassFieldsDoesNotStopMergingScalarFieldsForToManyUninitializedAssociations() { $id = 2; - $this->createChild($id, DDC3699RelationMany::CLASSNAME, 'addRelation'); + $child = new DDC3699Child(); + + $child->id = $id; + $child->childField = 'childValue'; + $child->parentField = 'parentValue'; + + $relation = new DDC3699RelationMany(); + + $relation->id = $id; + $relation->child = $child ; + $child->relations[] = $relation; + + $this->_em->persist($relation); + $this->_em->persist($child); + $this->_em->flush(); + $this->_em->clear(); + + /* @var $unmanagedChild DDC3699Child */ $unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); $this->_em->detach($unmanagedChild); // make it managed again $this->_em->find(DDC3699Child::CLASSNAME, $id); - $unmanagedChild->setChildField('modifiedChildValue'); - $unmanagedChild->setParentField('modifiedParentValue'); + $unmanagedChild->childField = 'modifiedChildValue'; + $unmanagedChild->parentField = 'modifiedParentValue'; + /* @var $mergedChild DDC3699Child */ $mergedChild = $this->_em->merge($unmanagedChild); - $this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); - $this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); + $this->assertSame($mergedChild->childField, 'modifiedChildValue'); + $this->assertSame($mergedChild->parentField, 'modifiedParentValue'); } } \ No newline at end of file From 173729e5608c065f347c2b078c17d8dfee34cf7d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 15 Jul 2015 21:47:37 +0100 Subject: [PATCH 4/5] DDC-3699 - #1387 - catching specific exceptions --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php index c3366ec81..58f67dd3c 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php @@ -1,5 +1,6 @@ _em->getClassMetadata(DDC3699RelationMany::CLASSNAME), $this->_em->getClassMetadata(DDC3699Child::CLASSNAME), )); - } catch (\Exception $e) { + } catch (SchemaException $e) { // should throw error on second because schema is already created } } From 6bc405455ec6c397139189a5f0be918cc2f48399 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 15 Jul 2015 21:51:04 +0100 Subject: [PATCH 5/5] DDC-3699 - #1387 - leveraging the `OrmFunctionalTestCase` API --- .../Tests/ORM/Functional/Ticket/DDC3699Test.php | 13 ++----------- tests/Doctrine/Tests/OrmFunctionalTestCase.php | 6 ++++++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php index 58f67dd3c..a2eff6b57 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php @@ -13,18 +13,9 @@ class DDC3597Test extends \Doctrine\Tests\OrmFunctionalTestCase { protected function setUp() { - parent::setUp(); + $this->useModelSet('ddc3699'); - try { - $this->_schemaTool->createSchema(array( - $this->_em->getClassMetadata(DDC3699Parent::CLASSNAME), - $this->_em->getClassMetadata(DDC3699RelationOne::CLASSNAME), - $this->_em->getClassMetadata(DDC3699RelationMany::CLASSNAME), - $this->_em->getClassMetadata(DDC3699Child::CLASSNAME), - )); - } catch (SchemaException $e) { - // should throw error on second because schema is already created - } + parent::setUp(); } /** diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index f48d635f2..855fac617 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -139,6 +139,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\DDC117\DDC117Editor', 'Doctrine\Tests\Models\DDC117\DDC117Link', ), + 'ddc3699' => array( + 'Doctrine\Tests\Models\DDC3699\DDC3699Parent', + 'Doctrine\Tests\Models\DDC3699\DDC3699RelationOne', + 'Doctrine\Tests\Models\DDC3699\DDC3699RelationMany', + 'Doctrine\Tests\Models\DDC3699\DDC3699Child', + ), 'stockexchange' => array( 'Doctrine\Tests\Models\StockExchange\Bond', 'Doctrine\Tests\Models\StockExchange\Stock',