mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Immutable Entities #5353
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 @davidbarratt on GitHub (Dec 20, 2016).
Originally assigned to: @lcobucci on GitHub.
Is there a way to have immutable entities? My problem is that for instance if you create an entity and then persist it, Doctrine wants to set the id of the object.
Is there a way to have the persist method return a new object with the id on it?
@lcobucci commented on GitHub (Dec 20, 2016):
@davidbarratt if you want that you should not use a generated ID.
Maybe using an UUID would be better for you, you set it upfront (e.g. constructor) and the ORM would only use that value.
@davidbarratt commented on GitHub (Dec 20, 2016):
@lcobucci well that's an interesting solution I hadn't considered. I suppose objects loaded from the database just overload the properties on creation? Or do they need setters?
@JCMais commented on GitHub (Dec 20, 2016):
@davidbarratt reflection is used, no need for setters:
cd1a5fcadc/lib/Doctrine/ORM/UnitOfWork.php (L2578)@davidbarratt commented on GitHub (Dec 20, 2016):
@JCMais ok great! so the only feature that is really missing, is being able to use auto generated ids, but it's good to know there is a workaround.
@davidbarratt commented on GitHub (Dec 20, 2016):
Or rather... I should say that you cannot use immutable entities with
@GeneratedValue()since that would mutate your object, correct?@lcobucci commented on GitHub (Dec 20, 2016):
Indeed, generated identifiers are also configured via reflection but in the end it would change the state of the object after it's creation (which removes the whole immutable idea).
@lcobucci commented on GitHub (Dec 20, 2016):
With that said we'll close this as
invalid(since there's nothing to be done on the ORM) 😉@davidbarratt commented on GitHub (Dec 20, 2016):
@lcobucci I guess the only way around that would be for Doctrine to generate / return a new object? Can I open a feature request for an option to force Doctrine to do this?
@lcobucci commented on GitHub (Dec 20, 2016):
@davidbarratt the ORM is not responsible for creating a new object, it just gets an object that the user created and persists it (following the provided mapping).
You can definitely open the feature request but I don't think it would be implemented/accepted. The idea couples your domain to a detail (persistence), which is not something good when talking about object oriented design and architecture.
It would also push some extra complexity to the ORM, which is something that we always try to avoid 😃.
@davidbarratt commented on GitHub (Dec 20, 2016):
@lcobucci which is worse, the coupling (domain to a detail), or mutable data in OOP?
@lcobucci commented on GitHub (Dec 20, 2016):
@davidbarratt IMHO coupling (specially when talking about your domain). Mutability creates complexity which is not necessarily a problem.
@Ocramius commented on GitHub (Dec 23, 2016):
Actually very simple to implement: use
@Entity(readOnly=true):-)The rest (API-wise) is up to you, sorry.