mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Allow way to clear cached persisters on UnitOfWork
#6854
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 @Jasonoro on GitHub (Oct 11, 2021).
Feature Request
Summary
Currently, when calling
clear()on aUnitOfWork, the cachedpersistersaren't cleared, nor is there any other way of clearing this cache. However, clearing this cache is needed in some scenario's. Most commonly, it is needed after setting the class metadata manually, for example when (temporarily) switching from an assigned ID to a manual ID [1] [2]. For example, the following code fails:The above code would work fine if the
persistersfield would be cleared. As such, I see two options:UnitOfWork->clear(). To make the above code work by inserting a simple$manager->clear(). However, in theory this could break BC, if someone is relying on the caching logic somehow.clearPersisters()method. This would not BC break, but looks very out-of-place and wouldn't be my preferred solution.I can make a PR for this, but some discussion about whether we want to clear this cache might be good first.
@beberlei commented on GitHub (Oct 29, 2021):
This is not a supported use case to modify the metadata at runtime. Can you explain what you are using this for? I am inclined to close this, because its really causing havoc to do this. Maybe you should create a custom id generator?
@Jasonoro commented on GitHub (Nov 22, 2021):
Apologies for the late reaction, missed the comment.
The use case for this is that normally the id is auto-assigned. However, when migrating an old database over to a new one, the old records need to keep their original ID, while after that step some new records are inserted that have a auto-generated ID. As such, the above code was created (and the bug was found).
Looking back on it, it might be a better idea to make a custom ID generator that auto-generates if there is no ID set, and if there is an ID set it just takes that one. That might just be a better solution here.
If this isn't supported, feel free to close the issue. A small note in the documentation might however be useful, as these are all public methods.