Skip to content
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
15 changes: 12 additions & 3 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use RuntimeException;
use Stringable;
use Symfony\Component\VarExporter\Hydrator;
use Throwable;
use UnexpectedValueException;

use function array_chunk;
Expand Down Expand Up @@ -3054,16 +3055,24 @@ public function initializeObject(object $obj): void
}

if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
$reflection = $this->em->getClassMetadata($obj::class)->getReflectionClass();
$reflection->initializeLazyObject($obj);
try {
$reflection = $this->em->getClassMetadata($obj::class)->getReflectionClass();
$reflection->initializeLazyObject($obj);
} catch (Throwable) {
// No-op for non-Doctrine entities according to the ObjectManager::initializeObject() interface documentation.
}
}
}

/** Tests if a value is an uninitialized entity. */
public function isUninitializedObject(mixed $obj): bool
{
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled() && ! ($obj instanceof Collection) && is_object($obj)) {
return $this->em->getClassMetadata($obj::class)->reflClass->isUninitializedLazyObject($obj);
try {
return $this->em->getClassMetadata($obj::class)->reflClass->isUninitializedLazyObject($obj);
} catch (Throwable) {
return false;
}
}

return $obj instanceof InternalProxy && ! $obj->__isInitialized();
Expand Down
84 changes: 84 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12172Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use DateTime;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
use stdClass;

#[Group('GH12172')]
class GH12172Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(CmsUser::class);
}

public function testInitializeObjectNoOpForNonManagedObjects(): void
{
$nonEntity = new stdClass();
$nonEntity->someProperty = 'test';

$this->expectNotToPerformAssertions();
$this->_em->initializeObject($nonEntity);
}

public function testInitializeObjectNoOpForCustomNonEntityClass(): void
{
$nonEntity = new GH12172TestClass();
$nonEntity->data = 'some data';

$this->expectNotToPerformAssertions();
$this->_em->initializeObject($nonEntity);
}

public function testIsUninitializedObjectNoExceptionForNonManagedObjects(): void
{
$nonEntity = new stdClass();

$result = $this->_em->isUninitializedObject($nonEntity);

self::assertFalse($result, 'Non-managed objects should be considered initialized (not uninitialized)');
}

public function testInitializeObjectWithValidEntityClassDoesNotBreakMetadataLoading(): void
{
$user = new CmsUser();
$user->name = 'Test User';
$user->username = 'testuser';
$user->status = 'active';

$this->expectNotToPerformAssertions();
$this->_em->initializeObject($user);
}

public function testInitializeObjectWithDifferentTypesOfObjectsConsistentBehavior(): void
{
$testObjects = [
new stdClass(),
new GH12172TestClass(),
new DateTime(),
(object) ['prop' => 'value'],
];

foreach ($testObjects as $object) {
$this->_em->initializeObject($object);

$result = $this->_em->isUninitializedObject($object);
self::assertFalse($result, 'Non-entity objects should not be considered uninitialized');
}

$this->addToAssertionCount(1);
}
}

class GH12172TestClass
{
public string $data = '';
}