Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed proxy initialization for EnumReflectionProperty #11387

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
55 changes: 55 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EntityCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11386;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToOne;

#[Entity]
class GH11386EntityCart
{
#[Id]
#[GeneratedValue]
#[Column]
private int|null $id = null;

#[Column]
private int|null $amount = null;

#[OneToOne(inversedBy: 'cart', cascade: ['persist', 'remove'], orphanRemoval: true)]
private GH11386EntityCustomer|null $customer = null;

public function getId(): int|null
{
return $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11386;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToOne;

#[Entity]
class GH11386EntityCustomer
{
#[Id]
#[GeneratedValue]
#[Column]
private int|null $id = null;

#[Column]
private string|null $name = null;

#[Column(type: 'smallint', nullable: true, enumType: GH11386EnumType::class, options: ['unsigned' => 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;
}
}
11 changes: 11 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11386/GH11386EnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11386;

enum GH11386EnumType: int
{
case MALE = 1;
case FEMALE = 2;
}
39 changes: 39 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11386/GH11386Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11386;

use Doctrine\Tests\OrmFunctionalTestCase;

final class GH11386Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->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());
}
}