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

readonly support for identifiers #353

Closed
benblub opened this issue Nov 21, 2022 · 7 comments
Closed

readonly support for identifiers #353

benblub opened this issue Nov 21, 2022 · 7 comments

Comments

@benblub
Copy link
Contributor

benblub commented Nov 21, 2022

If you use readonly on the id/identifier an error is thrown. Use readonly on $title works as expected.

if we take a look into the code where the error is thrown..
if we look into the comparison, values are the same, so the object instance is not the same

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

Stacktrace

LogicException : Attempting to change readonly property App\Foo\Domain\Model\Foo::$id.
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ReflectionReadonlyProperty.php:48
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2753
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:266
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:492
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:148
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:270
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:841
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2227
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2199
 /app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:711
 /app/vendor/zenstruck/foundry/src/Proxy.php:168
 /app/vendor/zenstruck/foundry/src/Proxy.php:131

Model/Entity

#[ORM\Entity]
class Foo
{
    #[ORM\Id]
    #[ORM\Column(type: 'uuid', unique: true)]
    public readonly UuidInterface $id;

    public function __construct(
        #[ORM\Embedded(columnPrefix: false)]
        public readonly Title $title,
    ) {
        $this->id = Uuid::uuid4();
    }
}

Factory

    protected function getDefaults(): array
    {
        return [
            'title' => new Title('Foo..'),
        ];
    }

    protected function initialize(): self
    {
        return $this->instantiateWith((new Instantiator())->alwaysForceProperties());
    }

i didn't looked yet deep dive into the code how foundry instantiate the object and whats the different there between normal props and the identifier. With PHP 8.2 we get immutable classes, this examble used readonly probs -> PHP 8.1

https://stitcher.io/blog/php-81-readonly-properties

@kbond
Copy link
Member

kbond commented Nov 22, 2022

I've definitely seem some problems with readonly properties in doctrine that aren't fixed.

Are you sure this is foundry-specific?

What's the code that causes the problem?

$object = FooFactory::create(); // ?

$object->id; // ?
$object->title; // ?

@kbond
Copy link
Member

kbond commented Nov 22, 2022

Does this work as expected (no foundry code):

$foo = new Foo(new Title('foo...));
$em->persist($foo);
$em->flush();

$em->refresh($foo);

@benblub
Copy link
Contributor Author

benblub commented Nov 22, 2022

Hi Kevin FooFactory::create(); throws the exception

i tested this without problems. Let me check refresh works or not.

$foo = new Foo(new Title('foo...));
$em->persist($foo);
$em->flush();

@benblub
Copy link
Contributor Author

benblub commented Nov 22, 2022

refresh works

@kbond
Copy link
Member

kbond commented Nov 22, 2022

Ok thanks, I'll have to dig into this.

@kbond
Copy link
Member

kbond commented Nov 22, 2022

refresh works

Per a slack conversation with @benblub, it does not. I think:

Could be related

@nikophil
Copy link
Member

nikophil commented Nov 22, 2022

In my experience, readonly is broken with value objects in Doctrine:

@benblub benblub closed this as completed Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants