fix: flush only once per calls to Factory::create()
in userland
#836
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ok, I think it's time for this now 😅
It's been a while that I wanted to achieve this: we're calling too many
flush()
, because we're flushing every time an entity is created, although, the "Doctrine way" would be to only flush once per "root factory".This is actually possible thanks to all the work that have been done around "schedule for insert" stuff.
A problem that could have happened because of this, is with "after persist" callbacks : not calling
save()
for every entity would mean not calling their "after persist callbacks", but I was pleasantly surprised that this is an already solved problem, thanks to this PR 🎉What triggered me to do this now, was this issue raised by @mmarton
given this code:
there is a problem with the "placeholder solution" for inverse
OneToOne
: at some point, because we flush right after thecategory
is created, and becausestock
is already persisted (the doctrine way), it tries to save the "placeholder" 🤷I've tried a lot of different things, but nothing works with every combination possible of "cascade persist" stuff. But the problem is naturally fixed when we deffer the
flush()
call: we can safely remove the placeholder from the UoW, before any flush attempt is made.Another solution would be to use PHP 8.4 proxies, because the placeholder could be the real object, but that's more work, and it will only fix the problem for the last PHP version. Moreover both of these solutions are not mutually exclusive, and I'm planning to work on this at some point,(maybe in the same time when I'll use PHP 8.4 proxies for data provider as well).
It comes also with a performance boost:
locally, without Mongo and with DAMA enabled, the command
phpunit --exclude-group maker
runs in 10s now and in 15s before 🏃 ⚡FYI, before the fix, the whole testsuite was performing 3040 calls to
flush()
, and now, only 1300Surprisingly, there is no performance boost in the testsuite without dama.
Finally, I think this should be released in
2.3.x
, because the 2.4 release will be full of new features, which are not 100% ready.I've discovered yet another problem with the "placeholder" stuff for inversed one-to-one, when not using auto-generated ids: in case of
cascade: persist
, the placeholder was passed to$em->persist()
, and it was emitting aEntityMissingAssignedId
because the id is generated in the constructor of the entity, but the "placeholder" is created withnewInstanceWithoutConstructor()
.Then I decided to even deffer
$em->persist()
calls. This has the main advantage that we can do all the weird stuff we want, and only after that, doctrine joins the party. With the solution, I think we will finally be done with all that "inverse one to one" madness 🎉 😅