From 67ac5a82dab0b76764fedb469202006f3c80b60b Mon Sep 17 00:00:00 2001 From: Valentin Karnauhov Date: Wed, 20 Mar 2024 17:59:34 +0200 Subject: [PATCH] Fixed proxy initialization for EnumReflectionProperty --- src/Proxy/ProxyFactory.php | 2 +- .../Ticket/GH11386/GH11386EntityCart.php | 55 +++++++++++++ .../Ticket/GH11386/GH11386EntityCustomer.php | 80 +++++++++++++++++++ .../Ticket/GH11386/GH11386EnumType.php | 11 +++ .../Functional/Ticket/GH11386/GH11386Test.php | 39 +++++++++ 5 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCart.php create mode 100644 tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCustomer.php create mode 100644 tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EnumType.php create mode 100644 tests/Tests/ORM/Functional/Ticket/GH11386/GH11386Test.php diff --git a/src/Proxy/ProxyFactory.php b/src/Proxy/ProxyFactory.php index f784cefa651..8dd39ddd868 100644 --- a/src/Proxy/ProxyFactory.php +++ b/src/Proxy/ProxyFactory.php @@ -234,7 +234,7 @@ private function createLazyInitializer(ClassMetadata $classMetadata, EntityPersi $class = $entityPersister->getClassMetadata(); foreach ($class->getReflectionProperties() as $property) { - if (! $property || ! $class->hasField($property->name) && ! $class->hasAssociation($property->name)) { + if (! $property || ! $class->hasField($property->getName()) && ! $class->hasAssociation($property->getName())) { continue; } diff --git a/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCart.php b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCart.php new file mode 100644 index 00000000000..032171aac52 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCart.php @@ -0,0 +1,55 @@ +id; + } + + public function getAmount(): int|null + { + return $this->amount; + } + + public function setAmount(int $amount): static + { + $this->amount = $amount; + + return $this; + } + + public function getCustomer(): GH11386EntityCustomer|null + { + return $this->customer; + } + + public function setCustomer(GH11386EntityCustomer|null $customer): self + { + $this->customer = $customer; + + return $this; + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCustomer.php b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCustomer.php new file mode 100644 index 00000000000..3290a6f99bf --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCustomer.php @@ -0,0 +1,80 @@ + true])] + private GH11386EnumType|null $type = null; + + #[OneToOne(mappedBy: 'customer')] + private GH11386EntityCart|null $cart = null; + + public function getId(): int|null + { + return $this->id; + } + + public function getName(): string|null + { + return $this->name; + } + + public function setName(string $name): static + { + $this->name = $name; + + return $this; + } + + public function getType(): GH11386EnumType|null + { + return $this->type; + } + + public function setType(GH11386EnumType $type): static + { + $this->type = $type; + + return $this; + } + + public function getCart(): GH11386EntityCart|null + { + return $this->cart; + } + + public function setCart(GH11386EntityCart|null $cart): self + { + // unset the owning side of the relation if necessary + if ($cart === null && $this->cart !== null) { + $this->cart->setCustomer(null); + } + + // set the owning side of the relation if necessary + if ($cart !== null && $cart->getCustomer() !== $this) { + $cart->setCustomer($this); + } + + $this->cart = $cart; + + return $this; + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EnumType.php b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EnumType.php new file mode 100644 index 00000000000..c07da865b95 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EnumType.php @@ -0,0 +1,11 @@ +createSchemaForModels( + GH11386EntityCart::class, + GH11386EntityCustomer::class, + ); + } + + public function testInitializeClonedProxy(): void + { + $cart = new GH11386EntityCart(); + $cart->setAmount(1000); + + $customer = new GH11386EntityCustomer(); + $customer->setName('John Doe') + ->setType(GH11386EnumType::MALE) + ->setCart($cart); + + $this->_em->persist($cart); + $this->_em->flush(); + $this->_em->clear(); + + $cart = $this->_em->find(GH11386EntityCart::class, 1); + $customer = clone $cart->getCustomer(); + self::assertEquals('John Doe', $customer->getName()); + } +}