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

Fix the initialization of lazy-ghost proxies with postLoad listeners #11544

Open
wants to merge 1 commit into
base: 2.19.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -447,7 +447,7 @@ private function getProxyFactory(string $className): Closure
foreach ($reflector->getProperties($filter) as $property) {
$name = $property->name;

if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name]))) {
if ($property->isStatic() || ! isset($identifiers[$name])) {
continue;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Tests/Models/GH11524/GH11524Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH11524;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="gh11524_entities")
*/
class GH11524Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int|null
*/
public $id = null;

/**
* @ORM\ManyToOne(targetEntity="GH11524Relation")
* @ORM\JoinColumn(name="relation_id", referencedColumnName="id", nullable=true)
*
* @var GH11524Relation|null
*/
public $relation = null;
}
21 changes: 21 additions & 0 deletions tests/Tests/Models/GH11524/GH11524Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH11524;

use Doctrine\ORM\Event\PostLoadEventArgs;

class GH11524Listener
{
public function postLoad(PostloadEventArgs $eventArgs): void
{
$object = $eventArgs->getObject();

if (!$object instanceof GH11524Relation) {

Check failure on line 15 in tests/Tests/Models/GH11524/GH11524Listener.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Expected 1 space(s) after NOT operator; 0 found
return;
}

$object->setCurrentLocale('en');
}
}
50 changes: 50 additions & 0 deletions tests/Tests/Models/GH11524/GH11524Relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH11524;

use Doctrine\ORM\Mapping as ORM;
use LogicException;

/**
* @ORM\Entity
* @ORM\Table(name="gh11524_relations")
*/
class GH11524Relation
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int|null
*/
public $id;

/**
* @ORM\Column(type="string")
*
* @var string
*/
public $name;

/**
* @var string|null
*/
private $currentLocale;

public function setCurrentLocale(string $locale): void
{
$this->currentLocale = $locale;
}

public function getTranslation(): string
{
if ($this->currentLocale === null) {
throw new LogicException('The current locale must be set to retrieve translation.');
}

return 'fake';
}
}
51 changes: 51 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11524Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Events;
use Doctrine\Persistence\Proxy;
use Doctrine\Tests\Models\GH11524\GH11524Entity;
use Doctrine\Tests\Models\GH11524\GH11524Listener;
use Doctrine\Tests\Models\GH11524\GH11524Relation;
use Doctrine\Tests\OrmFunctionalTestCase;

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

$this->createSchemaForModels(
GH11524Entity::class,
GH11524Relation::class
);

$this->_em->getEventManager()->addEventListener(Events::postLoad, new GH11524Listener());
}

public function testPostLoadCalledOnProxy(): void
{
$relation = new GH11524Relation();

Check failure on line 30 in tests/Tests/ORM/Functional/Ticket/GH11524Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
$relation->name = 'test';
$this->_em->persist($relation);

$entity = new GH11524Entity();

Check failure on line 34 in tests/Tests/ORM/Functional/Ticket/GH11524Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space
$entity->relation = $relation;

$this->_em->persist($entity);
$this->_em->flush();

$this->_em->clear();

$reloadedEntity = $this->_em->find(GH11524Entity::class, $entity->id);

$reloadedRelation = $reloadedEntity->relation;

$this->assertInstanceOf(Proxy::class, $reloadedRelation, 'The reloaded relation must be a proxy');
$this->assertFalse($reloadedRelation->__isInitialized());

$this->assertSame('fake', $reloadedRelation->getTranslation(), 'The property set by the postLoad listener must get initialized on usage.');
}
}
Loading