mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Unable to use readonly modifier for Collection properties #7139
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @janedbal on GitHub (Apr 27, 2023).
Bug Report
Doctrine fails to replace ArrayCollection with PersistentCollection for readonly properties. I assume this should be documented or bypassed (cannot imagine how).
Current docs states it should be possible.:
Current behavior
It throws exception, callstack:
How to reproduce
Test:
Entities:
Expected behavior
No failure
@mpdude commented on GitHub (May 31, 2023):
When a new entity instance is created – and not yet persisted – you need to initialize the collection attribute with
ArrayCollection, like in your example given.At least when the entity is persisted for the first time, this
ArrayCollectionneeds to be replaced by aPersistentCollection.I don't see a way in PHP's reflection API to modify
readonlyproperties.So, my (personal) conclusion would be that this is a technical limitation that we cannot overcome, and it should be documented as such.
I don't know if it is worthwhile to add runtime checks catching this and giving a more explicit error message; users are going to notice anyway their code does not work.
@vnivuahc commented on GitHub (Aug 23, 2025):
I encountered the same issue with a OneToMany relationship.
Changing
to
seems to avoid the error, with PHP8.4.
This is not as explicit and handy than
readonlybut it is worth than nothing.EDITED after the issue was closed : I fixed my code sample. I meant
public private(set), notprivate private(set)@derrabus commented on GitHub (Aug 23, 2025):
Yes because it's the same as simply removing the
readonlyflag.I'd like to close the issue as won't fix. The ORM needs to be able to replace the collection when persisting a new entity. Therefore,
readonlydoes not make sense here. If somebody feels like this should be documented better, please send a PR.