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

[DDC-3378] Support merging entities with composite identities defined through to-one associations #1176

Closed
wants to merge 3 commits into from

Conversation

adrienbrault
Copy link
Contributor

I hope that I'm doing something wrong and that this is not a bug ...

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DDC-3378

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2014

@adrienbrault there is a class naming collision in the test case.

$state->country = $country;

$this->_em->merge($country);
$this->_em->merge($state);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that the following works:

$country = new MergeCompositeToOneKeyCountry();
$country->country = 'US';
$state = new MergeCompositeToOneKeyState();
$state->state = 'CA';
$state->country = $country;

$newCountry = $this->_em->merge($country);
$state->country = $newCountry;
$this->_em->merge($state);

Copy link
Member

Choose a reason for hiding this comment

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

Well, merge() does not make the argument it receives managed. It returns a new instance which is managed (because it might already have it in its identity map).

So you issue is a wrong usage of merge()

Copy link
Member

Choose a reason for hiding this comment

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

note that Doctrine can be configured to cascade the merge operation, which could help you in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof I've updated the test to a valid use case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that the cascading merges happens after uow tries to get the State id, and at that point the Country is not managed

@adrienbrault
Copy link
Contributor Author

@Ocramius Updated

@@ -59,12 +59,8 @@ public function generate(EntityManager $em, $entity)
}

if (isset($class->associationMappings[$idField])) {
if ( ! $em->getUnitOfWork()->isInIdentityMap($value)) {
throw ORMException::entityMissingForeignAssignedId($entity, $value);
}
Copy link
Member

Choose a reason for hiding this comment

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

removing this check looks wrong to me. It is expected to enforce using managed entities

Copy link
Member

Choose a reason for hiding this comment

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

The issue when you remove this is that you now allow using unmanaged entities in identifiers even in contexts where it should not be allowed. Fixing the retrieval of the id when merging should not be done by opening all other cases for bugs.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is actually correct though - identities deriving from associations are still valid, even for un-managed entities.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, indeed. The relation itslef will still be required to use a managed entity in other places

@hjr3
Copy link

hjr3 commented Dec 20, 2014

I can reproduce this with the following use case:

    /** @var $obj \Doctrine\Tests\ORM\Functional\MergeCompositeToOneKeyState */
    $doctrine->detach($obj); 
    $em->clear(); 
    $doctrine->merge($obj);

The merge code tries to flatten a composite key with a foreign identifier but requires the entity identifier values that were just cleared.

@Ocramius Ocramius changed the title Add test exposing UnitOfWork merge bug [DDC-3378] [Test] Add test exposing UnitOfWork merge bug Jan 15, 2015
@Ocramius
Copy link
Member

@hjr3 does this apply also to the codebase after @adrienbrault's patch?

@Ocramius Ocramius changed the title [DDC-3378] [Test] Add test exposing UnitOfWork merge bug [DDC-3378] Add test exposing UnitOfWork merge bug Jan 16, 2015
@hjr3
Copy link

hjr3 commented Jan 17, 2015

@Ocramius the patch from @adrienbrault does address my issue.

@Ocramius
Copy link
Member

@adrienbrault can you check @stof's comment please?

@Ocramius
Copy link
Member

@adrienbrault merged, thanks!

master: b889e18

@Ocramius Ocramius changed the title [DDC-3378] Add test exposing UnitOfWork merge bug [DDC-3378] Support merging entities with composite identities defined through to-one associations Jan 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants