mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Extra update for self-referencing Many-To-One with NONE generator strategy during persist #6332
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 @sylfabre on GitHub (Oct 23, 2019).
Bug Report
Summary
Using a self-referencing entity with a Many-To-One relationship set during the
__construct()call with aNONEstrategy for id generation leads to two queries:Current behavior
NULLfor the firstINSERT[2019-10-23 16:04:21] doctrine.DEBUG: INSERT INTO organization (id, legal_parent_id) VALUES (?, ?, ?, ?, ?, ?, ?) {"1":"[object] (Ramsey\\Uuid\\Uuid: \"c68555b0-f5ae-11e9-914e-38baf82a0624\")","2":null} []UPDATEquery[2019-10-23 16:04:21] doctrine.DEBUG: UPDATE organization SET legal_parent_id = ? WHERE id = ? ["[object] (Ramsey\\Uuid\\Uuid: \"c68555b0-f5ae-11e9-914e-38baf82a0624\")","[object] (Ramsey\\Uuid\\Uuid: \"c68555b0-f5ae-11e9-914e-38baf82a0624\")"] []How to reproduce
Expected behavior
INSERTquery withlegal_parent_idnotNULL[2019-10-23 16:04:21] doctrine.DEBUG: INSERT INTO organization (id, legal_parent_id) VALUES (?, ?, ?, ?, ?, ?, ?) {"1":"[object] (Ramsey\\Uuid\\Uuid: \"c68555b0-f5ae-11e9-914e-38baf82a0624\")","2":[object] (Ramsey\\Uuid\\Uuid: \"c68555b0-f5ae-11e9-914e-38baf82a0624\")} []Possible solution
This comes from
vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.phpWe could check if
spl_object_hash($newVal) === spl_object_hash($entity)and ifClassMetadataInfo::GENERATOR_TYPE_NONE === $this->class->generatorType. If yes, then do not set$newVal = null;and do not schedule extra update.I can provide a PR if you want
@SenseException commented on GitHub (Oct 29, 2019):
Thank you for opening this issue. Pull requests are welcome if you like to provide one. The PR will also need integration test(s) with an entity close to the one of your example, but with a classic integer id. I assume your example was close to the actual use case of the project you're working on.
@sylfabre commented on GitHub (Oct 30, 2019):
@SenseException PR done https://github.com/doctrine/orm/pull/7882
@mpdude commented on GitHub (Jun 1, 2023):
Out of curiosity, do you really care about one UPDATE query more or less?
Or is the real motivation that a self-referencing (at the class level) association like this has to be defined as NULLable just because of the edge case that an entity might reference itself (at the entity level)?
I find the latter argument much stronger.
If that’s the case, we should also write the tests in a way to show that the entities can be inserted even with a non-NULLable association, not focus so much on the query count.
@sylfabre commented on GitHub (Jun 1, 2023):
@mpdude The real motivation is having to define a NULLable column.
You're right about the test, I'll work on a better version in my PR
@sylfabre commented on GitHub (Jul 11, 2023):
Fixed by https://github.com/doctrine/orm/pull/10735