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 entity refresh after cleared em #10362

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
Expand Up @@ -165,6 +165,10 @@ protected function hydrateRowData(array $row, array &$result)
}
}

if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
$this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
}

$uow = $this->_em->getUnitOfWork();
$entity = $uow->createEntity($entityName, $data, $this->_hints);

Expand Down
56 changes: 47 additions & 9 deletions lib/Doctrine/ORM/Proxy/ProxyFactory.php
Expand Up @@ -18,10 +18,12 @@
use Doctrine\ORM\Utility\IdentifierFlattener;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Proxy;
use ReflectionClass;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\VarExporter;

use function array_flip;
use function array_merge;
use function str_replace;
use function strpos;
use function substr;
Expand Down Expand Up @@ -311,10 +313,39 @@ private function generateUseLazyGhostTrait(ClassMetadata $class): string

private function generateSkippedProperties(ClassMetadata $class): string
{
$skippedProperties = ['__isCloning' => true];
$identifiers = array_flip($class->getIdentifierFieldNames());
$skippedProperties = array_merge(
['__isCloning' => true],
$this->collectClassSkippedProperties(
$class->getReflectionClass(),
$class,
array_flip($class->getIdentifierFieldNames())
)
);

uksort($skippedProperties, 'strnatcmp');

$code = VarExporter::export($skippedProperties);

return str_replace([VarExporter::export($class->getName()), "\n"], ['parent::class', "\n "], $code);
}

/**
* @psalm-param array<string, int> $identifiers

* @psalm-return array<string, true>
*/
private function collectClassSkippedProperties(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Straight forward solution coming into my mind first but really not the most elegant one. A pity the already available field mapping of ClassMetadataInfo cannot be used. It just misses a few information like private visibility of the properties.

Another much easier fix could be to make sure the identifier is in the skip array after the loop. If this is not the case we should be able to consider it private and therefor have all information to add it. All other private properties of parent classes should not matter anyway, if I got that correctly.

ReflectionClass $refClass,
ClassMetadata $class,
array $identifiers,
bool $processPrivatePropertiesOnly = false
): array {
$skippedProperties = [];
foreach ($refClass->getProperties() as $property) {
if ($processPrivatePropertiesOnly && ! $property->isPrivate()) {
continue;
}

foreach ($class->getReflectionClass()->getProperties() as $property) {
$name = $property->getName();

if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name]))) {
Expand All @@ -326,13 +357,20 @@ private function generateSkippedProperties(ClassMetadata $class): string
$skippedProperties[$prefix . $name] = true;
}

uksort($skippedProperties, 'strnatcmp');

$code = VarExporter::export($skippedProperties);
$code = str_replace(VarExporter::export($class->getName()), 'parent::class', $code);
$code = str_replace("\n", "\n ", $code);
$parentClass = $refClass->getParentClass();
if ($parentClass !== false) {
simonberger marked this conversation as resolved.
Show resolved Hide resolved
return array_merge(
$skippedProperties,
$this->collectClassSkippedProperties(
$parentClass,
$class,
$identifiers,
true
)
);
}

return $code;
return $skippedProperties;
}

private function generateSerializeImpl(ClassMetadata $class): string
Expand Down
27 changes: 27 additions & 0 deletions tests/Doctrine/Tests/Models/GH10336/GH10336Entity.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH10336;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="gh10336_entities")
*/
class GH10336Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
public ?int $id = null;

/**
* @ORM\ManyToOne(targetEntity="GH10336Relation")
* @ORM\JoinColumn(name="relation_id", referencedColumnName="id", nullable=true)
*/
public ?GH10336Relation $relation = null;
}
26 changes: 26 additions & 0 deletions tests/Doctrine/Tests/Models/GH10336/GH10336Relation.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH10336;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="gh10336_relations")
*/
class GH10336Relation
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
public ?int $id = null;

/**
* @ORM\Column(type="string")
*/
public string $value;
}
44 changes: 44 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH10336Test.php
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\GH10336\GH10336Entity;
use Doctrine\Tests\Models\GH10336\GH10336Relation;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @requires PHP 7.4
*/
final class GH10336Test extends OrmFunctionalTestCase
{
public function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
GH10336Entity::class,
GH10336Relation::class
);
}

public function testCanAccessRelationPropertyAfterClear(): void
{
$relation = new GH10336Relation();
$relation->value = 'foo';
$entity = new GH10336Entity();
$entity->relation = $relation;

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

$entity = $this->_em->find(GH10336Entity::class, 1);

$this->_em->clear();

$this->assertSame('foo', $entity->relation->value);
}
}