From 43ce0bef78618494058899d03f5a96b59d1f8587 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Mon, 26 Sep 2022 12:53:37 +0200 Subject: [PATCH 1/2] lazy and eager collection refresh --- .../Ticket/LazyEagerCollectionTest.php | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php new file mode 100644 index 0000000000..2797dfd67b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php @@ -0,0 +1,160 @@ +createSchemaForModels( + LazyEagerCollectionUser::class, + LazyEagerCollectionAddress::class, + LazyEagerCollectionPhone::class + ); + } + + public function testRefreshRefreshesBothLazyAndEagerCollections(): void + { + $user = new LazyEagerCollectionUser(); + $user->data = 'Guilherme'; + + $ph = new LazyEagerCollectionPhone(); + $ph->data = '12345'; + $user->addPhone($ph); + + $ad = new LazyEagerCollectionAddress(); + $ad->data = '6789'; + $user->addAddress($ad); + + $this->_em->persist($user); + $this->_em->persist($ad); + $this->_em->persist($ph); + $this->_em->flush(); + $this->_em->clear(); + + $user = $this->_em->find(LazyEagerCollectionUser::class, $user->id); + $ph = $user->phones[0]; + $ad = $user->addresses[0]; + + $ph->data = 'abc'; + $ad->data = 'def'; + + $this->_em->refresh($user); + + self::assertSame('12345', $ph->data); + self::assertSame('6789', $ad->data); + } +} + +/** + * @Entity + */ +class LazyEagerCollectionUser +{ + /** + * @var int + * @Id + * @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @var string + * @Column(type="string", length=255) + */ + public $data; + + /** + * @ORM\OneToMany(targetEntity="LazyEagerCollectionPhone", cascade={"refresh"}, fetch="EAGER", mappedBy="user") + * + * @var LazyEagerCollectionPhone[] + */ + public $phones; + + /** + * @ORM\OneToMany(targetEntity="LazyEagerCollectionAddress", cascade={"refresh"}, mappedBy="user") + * + * @var LazyEagerCollectionAddress[] + */ + public $addresses; + + public function __construct() + { + $this->addresses = new ArrayCollection(); + $this->phones = new ArrayCollection(); + } + + public function addPhone(LazyEagerCollectionPhone $phone): void + { + $phone->user = $this; + $this->phones[] = $phone; + } + + public function addAddress(LazyEagerCollectionAddress $address): void + { + $address->user = $this; + $this->addresses[] = $address; + } +} + +/** @Entity */ +class LazyEagerCollectionPhone +{ + /** + * @var int + * @Id + * @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @var string + * @Column(type="string", length=255) + */ + public $data; + + /** + * @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="phones") + * + * @var LazyEagerCollectionUser + */ + public $user; +} + +/** @Entity */ +class LazyEagerCollectionAddress +{ + /** + * @var int + * @Id + * @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @var string + * @Column(type="string", length=255) + */ + public $data; + + /** + * @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="addresses") + * + * @var LazyEagerCollectionUser + */ + public $user; +} From 7d1b24f3b1fde6e12385c299ba6ea4b0da385031 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Mon, 26 Sep 2022 13:16:38 +0200 Subject: [PATCH 2/2] attempt a fix --- src/UnitOfWork.php | 4 ++-- .../Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index 2969a3e0e0..0f775910d8 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -2473,13 +2473,13 @@ private function doRefresh($entity, array &$visited, ?int $lockMode = null): voi throw ORMInvalidArgumentException::entityNotManaged($entity); } + $this->cascadeRefresh($entity, $visited, $lockMode); + $this->getEntityPersister($class->name)->refresh( array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), $entity, $lockMode ); - - $this->cascadeRefresh($entity, $visited, $lockMode); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php index 2797dfd67b..926e052315 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php @@ -17,6 +17,7 @@ class LazyEagerCollectionTest extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); + $this->createSchemaForModels( LazyEagerCollectionUser::class, LazyEagerCollectionAddress::class,