How to make Doctrine commit in the correct order? #10026
Unanswered
NotionCommotion
asked this question in
Support Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've struggled with this for days and first thought it was some stupid mistake on my side, but now think it is "maybe" a bug but more likely a missing feature. My hopes are to better educate myself via this discussion and then post in the appropriate category.
Let me explain. Working on a multitenancy application where all entities (except tenants) must belong to a single tenant. To better isolate where I am in need of help, I am limiting the entities to the following:
Both tenants and vendors are organizations, and all users who belong to a given organization must have a unique email and username. I don't expect that there need be a relationship between some entities and all organizations, but there definitely will be one between other entities and all users (i.e. both tenant and vendor users). As such, class inheritance should be used for users so other entities maybe be joined. One use in particular will be the createBy and updateBy properties managed by the Blamable Doctrine Extension.
Sounds easy enough, right?
I've tried using several different schemas to implement this (see ERDs below and entities/SQL available if needed), however, for all of them,
UnitOfWork::getCommitOrder()
causes the VendorUser to be committed before the Vendor which obviously causes an error.I've spent a fair amount of time going over the CommitOrderCalculator source code, however, don't really know why it returns vendor user before vendor. On my real application which had more tables, it actually said to commit concrete classes before the parent classes which doesn't make sense.
CommitOrderCalculator implements topological sorting, which is an ordering algorithm for directed graphs (DG) and/or directed acyclic graphs (DAG) by using a depth-first searching (DFS) to traverse the graph built in memory. This algorithm have a linear running time based on nodes (V) and dependency between the nodes (E), resulting in a computational complexity of O(V + E).
So, maybe the above behavior is a bug, but more likely there is no way Doctrine could determine the correct order (which I think is still a bug if it just guesses, and at the very least how it guesses should be better described). It knows that vendor user references a vendor, but also that vendor references a user, so what to do? I, however, know that the user already exists because how else could it be creating some other entity. I could tell Doctrine to persist entities in whatever order I wish, but it does not follow this same order for actually committing them. Is there a way to instruct Doctrine to commit a given entity before another? I couldn't find a means and if there isn't, this would be a feature request.
Thank you in advance for any help.
Original attempt where class inheritance was used for organizations and users belong to organizations.
Second attempt where class inheritance was used for organizations but users belong to their specific organizations (i.e. tenant or vendor).
Third attempt where class inheritance was NOT used for organizations and users belong to their specific organizations.
Beta Was this translation helpful? Give feedback.
All reactions