orm: "LogicException: Attempting to change readonly property ..." for entity's Id during proxy initialization

Bug Report

Q A
BC Break no
Version 2.12.3

Summary

When initializing a proxy by accessing a non-loaded property, if the Id is set as readonly, a LogicException gets thrown.

Probably similar to #9538, but in a different code path. I ran into this by directly using getReference(), but I suspect the behavior would be exhibited on any proxy relation.

Current behavior

LogicException: Attempting to change readonly property Firehed\Entities\Feed::$id. in /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ReflectionReadonlyProperty.php:48

Stack:

reader-php_fpm-1    | #0 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2745): Doctrine\ORM\Mapping\ReflectionReadonlyProperty->setValue(Object(DoctrineProxies\__CG__\Firehed\Entities\Feed), '2')
reader-php_fpm-1    | #1 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php(156): Doctrine\ORM\UnitOfWork->createEntity('Firehed\\Entitie...', Array, Array)
reader-php_fpm-1    | #2 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php(63): Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator->hydrateRowData(Array, Array)
reader-php_fpm-1    | #3 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php(270): Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator->hydrateAllData()
reader-php_fpm-1    | #4 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(757): Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll(Object(Doctrine\DBAL\Result), Object(Doctrine\ORM\Query\ResultSetMapping), Array)
reader-php_fpm-1    | #5 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(767): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->load(Array, Object(DoctrineProxies\__CG__\Firehed\Entities\Feed))
reader-php_fpm-1    | #6 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php(132): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadById(Array, Object(DoctrineProxies\__CG__\Firehed\Entities\Feed))
reader-php_fpm-1    | #7 /var/www/html/.generated/doctrine-proxies/__CG__FirehedEntitiesFeed.php(75): Doctrine\ORM\Proxy\ProxyFactory->Doctrine\ORM\Proxy\{closure}(Object(DoctrineProxies\__CG__\Firehed\Entities\Feed), '__get', Array)
reader-php_fpm-1    | #8 /var/www/html/.generated/doctrine-proxies/__CG__FirehedEntitiesFeed.php(75): Closure->__invoke(Object(DoctrineProxies\__CG__\Firehed\Entities\Feed), '__get', Array)
reader-php_fpm-1    | #9 /var/www/html/src/Api/MyStories.php(47): DoctrineProxies\__CG__\Firehed\Entities\Feed->__get('title')
reader-php_fpm-1    | #10 /var/www/html/public/index.php(71): Firehed\Api\MyStories->run(Array)
reader-php_fpm-1    | #11 {main}

How to reproduce

Entity:

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\{
    Column,
    Entity,
    GeneratedValue,
    Id,
    Table,
};

#[Entity]
#[Table(name: 'feeds')]
class Feed
{
    #[Id]
    #[GeneratedValue]
    #[Column(options: ['unsigned' => true], type: Types::BIGINT)]
    public readonly int $id;

    #[Column(options: ['default' => ''])]
    public string $title;
}

Table:

CREATE TABLE `feeds` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Code to cause error:

$reference = $em->getReference(Feed::class, 2);
var_dump($reference->id); // int(2)
var_dump($reference->title); // <-- crash here

Expected behavior

Data is loaded fine, var_dump shows the expected value from the db.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 13
  • Comments: 15

Most upvoted comments

Ran into the same problem with $id being a custom type build around a Symfony uuid. The cause seems to be the strict comparison in ReflectionReadonlyProperty.php:45

if (parent::getValue($objectOrValue) !== $value) {
    throw new LogicException(sprintf('Attempting to change readonly property %s::$%s.', $this->class, $this->name));
}

When I inspect the values in xdebug console, both represent the same uuid, but having different object id’s fails the comparison:

> parent::getValue($objectOrValue)
< App\Domain\Tag\TagGroupId::__set_state(array(
   'uuid' => 
  Symfony\Component\Uid\UuidV3::__set_state(array(
     'uid' => '3a705d0d-54e1-37b8-bd58-b4a188fa126a',
  )),
))

> $value
< App\Domain\Tag\TagGroupId::__set_state(array(
   'uuid' => 
  Symfony\Component\Uid\UuidV3::__set_state(array(
     'uid' => '3a705d0d-54e1-37b8-bd58-b4a188fa126a',
  )),
))

> spl_object_id(parent::getValue($objectOrValue))
< 4875

> spl_object_id($value)
< 4737

Hello, I face the same issue. Has somebody found a solution nor a workaround?

I suggest using this option until the problem is resolved. I think this is the most correct option at the moment, so that it can be quickly corrected with future updates:

    /**
     * @readonly impossible to specify "readonly" attribute natively due
     *           to a Doctrine feature/bug https://github.com/doctrine/orm/issues/9863
     */
    #[ORM\Id]
    #[ORM\Column(name: 'id', type: ExampleId::class)]
    public ExampleId $id;
    // or using PHP 8.4 features
    public private(set) ExampleId $id;