Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot persist new entity if primary key contains a foreign key and the referenced object is in state new and its id is not assigned #7003

Closed
wants to merge 6 commits into from

Conversation

NicolaF
Copy link

@NicolaF NicolaF commented Jan 23, 2018

As the id of the referenced entity is generated, when UnitOfWork#persistNew computes the primary key, it may contain some null values.

Because of 1cb8d79 , UnitOfWork#addToIdentityMap does not allow this anymore, which is a good thing.

We need to delay the insertion into identityMap and entityIdentifiers after the actual insert to ensure that we have a complete primary key, as we do with post-insert ID generators.

This kinda worked in 2.5, but in the end identityMap and entityIdentifiers contained wrong values anyway.

Nicolas FRANÇOIS added 3 commits January 22, 2018 19:10
…s and the target entity is new. This prevents a throw in UnitOfWork#addToIdentityMap because some fields are null.
@@ -1033,12 +1044,16 @@ private function executeInserts($class)
$persister = $this->getEntityPersister($className);
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postPersist);

$theseInsertions = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$theseInsertions is a bad name here

//add it now
$identifier = [];
$idFields = $class->getIdentifierFieldNames();
foreach ($idFields as $idField) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block seems to detect whether an identifier is incomplete. Can it be moved to a private method?

@@ -1067,6 +1082,34 @@ private function executeInserts($class)

$this->addToIdentityMap($entity);
}
} else {
foreach ($theseInsertions as $oid => $entity) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the commit order calculator is working correctly, then this should simply be pushed down in the execution path, no?

@Ocramius
Copy link
Member

Related: #7002
Related: #7006

Please check if #7006 fixes this for you.

@NicolaF
Copy link
Author

NicolaF commented Jan 26, 2018

Hi @Ocramius,
I'll make the requested changes.

This is actually not related to #7006. Here, the code path starts with UnitOfWork#persist
and what happens is:

  • set identifier if possible
  • schedule for insert
  • add to identity map if identifier has been set
  • cascade persist for all associations

The commit order calculator is not involved here, it is just basic tree walking.

Actually the commit order calculator does the job, otherwise the second part of the patch (in executeInserts) would not work : the referenced entity is correctly inserted first, and we can finally have a complete identifier for the referencing entity.

@Ocramius
Copy link
Member

Ocramius commented Jan 26, 2018 via email

@tmbdrogba
Copy link

Is this PR ready to be merged ?

@NicolaF
Copy link
Author

NicolaF commented Feb 12, 2018

It is IMHO, but I'm not the maintainer. @Ocramius, is it OK for you ?

@ste93cry
Copy link

Actually I think this is breaking a lot of projects as it's not possible anymore after upgrading to 2.6 to persist any entity related to a new one in the same batch. Can we get this merged or at least a feedback to get it merged asap?

@Ocramius
Copy link
Member

I'm simply busy with other work stuff ATM - don't expect any deep involved OSS work from me for a while, sorry.

@ste93cry
Copy link

Can we get at least another maintainer have a look at this? It's a really high priority bug to fix

@lcobucci
Copy link
Member

The code provided here doesn't follow our coding standards and we already had a test that reproduces this issue in #6701 (with a simpler set of entities too). I'll try to combine everything and adjust stuff to make this part of v2.6.1 (also checking for any kind of performance impact).

@lcobucci lcobucci assigned lcobucci and unassigned NicolaF Feb 19, 2018
@lcobucci lcobucci added this to the 2.6.1 milestone Feb 19, 2018
@lcobucci
Copy link
Member

Fix was ported to #6701 and everything works as expected (with a minor, acceptable, performance impact) and it will be released on v2.6.1 ASAP.

@NicolaF thank you for you help on the fix.

lcobucci added a commit that referenced this pull request Feb 19, 2018
Add failing tests for #6531 

Fixes #6043
Fixes #6531
Fixes #7002
Fixes #7003
@lcobucci lcobucci closed this Feb 19, 2018
@lcobucci lcobucci reopened this Feb 19, 2018
@lcobucci lcobucci closed this Feb 19, 2018
@ste93cry
Copy link

This is a huge news, thank you for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants