From ef08316b0c962a2f4affadc77caddd40427570cd Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Mon, 26 Sep 2022 12:53:37 +0200 Subject: [PATCH] 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 00000000000..2797dfd67bb --- /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; +}