Skip to content

Commit

Permalink
Fixed proxy initialization for EnumReflectionProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Karnauhov committed Mar 21, 2024
1 parent 55c4845 commit 67ac5a8
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Proxy/ProxyFactory.php
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
@@ -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;
}
}
@@ -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
@@ -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
@@ -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());
}
}

0 comments on commit 67ac5a8

Please sign in to comment.