mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
[DX] providing a id via constructor gets overwritten from @ORM\CustomIdGenerator #6257
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 @c33s on GitHub (Jun 24, 2019).
Originally assigned to: @lcobucci on GitHub.
Bug Report
doctrine/doctrine-bundle: 1.9.1
Summary
using
CustomIdGeneratorand additionally provide the id via constructor
Type.php:leads to
CustomIdGeneratoralways overwrite the id already set via constructor. the event calling the generator should do something llke checking ifidis null and only then call the generator.this would increase the UX/DX a lot.
Current behavior
on new entities the
CustomIdGeneratoralways "wins" which will create a new id even if already set via set or constructor.How to reproduce
create an entity using
CustomIdGeneratorand also allow the id to be set via construtorthe first dump will show the right id the 2nd will have a new generated id.
Expected behavior
if id is already set, do not create a new id
related issues: https://github.com/ramsey/uuid-doctrine/issues/81
for googles current suggested workarounds (quoted from symfony slack):
greg_wtm [7:35 PM]
greg_wtm [7:46 PM]
tarlepp [7:53 PM]
greg_wtm [7:58 PM]
@Ocramius commented on GitHub (Jun 24, 2019):
I'd rather say that either the identifier is generated or it isn't, not both scenarios.
@ramsey commented on GitHub (Jun 24, 2019):
Hypothetically, I might have a use-case where I want API clients to generate a UUID and send that as part of the body to create a new entity, but if they don't, I want to generate it. In this case, do I just turn off the custom generator and initialize it always in the constructor, as you see in @c33s's example?
@lcobucci commented on GitHub (Jun 25, 2019):
@ramsey I'd say so.
The idea of an identifier generator is to remove the need for the application to create ids. If your software has special requirements regarding that process, you should rather pull this responsibility from the ORM and provide the required logic.
@TomasVotruba commented on GitHub (Jul 12, 2019):
This is how we solved it: https://github.com/ramsey/uuid-doctrine/issues/81#issuecomment-510790171
@lcobucci commented on GitHub (Oct 2, 2019):
Closing here since this the designed and expected behaviour of the library and we don't plan to change (at least for now).