From fa3916c98cd4a36c7f577adc837b529d85a2a053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Wed, 29 Dec 2021 13:14:11 +0100 Subject: [PATCH] Fix original data incomplete after flush --- lib/Doctrine/ORM/UnitOfWork.php | 1 + tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 69 +++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 18541b5681c..13eb0194717 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -658,6 +658,7 @@ public function computeChangeSet(ClassMetadata $class, $entity) if ($class->isCollectionValuedAssociation($name) && $value !== null) { if ($value instanceof PersistentCollection) { if ($value->getOwner() === $entity) { + $actualData[$name] = $value; continue; } diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index f04068d3a35..df61cd27b59 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -828,6 +828,75 @@ public function testItThrowsWhenLookingUpIdentifierForUnknownEntity(): void $this->expectException(EntityNotFoundException::class); $this->_unitOfWork->getEntityIdentifier(new stdClass()); } + + /** + * @group #9300 + */ + public function testPersistedCollectionIsPresentInOriginalDataAfterComputeChangeset(): void + { + $entity = new EntityWithCollection(); + $entity->children->add(new ChildEntity()); + $entity->name = 'abc'; + + $this->_unitOfWork->persist($entity); + $this->_unitOfWork->commit(); + + $entity->name = 'abcd'; + $this->_unitOfWork->computeChangeSets(); + + self::assertArrayHasKey('children', $this->_unitOfWork->getOriginalEntityData($entity)); + } +} + +/** + * @Entity + */ +class EntityWithCollection +{ + /** + * @var Collection|ChildEntity[] + * @OneToMany(targetEntity="ChildEntity", mappedBy="parent", cascade={"persist"}) + */ + public $children; + + /** + * @var int + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + public $id; + + /** + * @var string + * @Column(type="string") + */ + public $name; + + public function __construct() + { + $this->children = new ArrayCollection(); + } +} + +/** + * @Entity + */ +class ChildEntity +{ + /** + * @var int + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + public $id; + + /** + * @var int + * @ManyToOne(targetEntity="EntityWithCollection", inversedBy="children") + */ + public $parent; } /**