I am having issues with the order of database commit queries. I have an entity A which has one or more B entities. What I'm trying to do is move B entities to a different A entity and then delete the B entry they were just moved from.
Example:
This is what my example entities look at prior to any manipulation
A (Mike)
- B (John)
- B (Dave)
A (Jake)
- B (Robert)
My code would look something like
// already fetched
$mike = $em->getRepository('A')->find(1);
$jake = $em->getRepository('A')->find(2);
foreach($jake->getBs() as $b) {
$b->setA($mike);
}
$em->persist($mike);
$em->remove($jake)
$em->flush();
So basically, I'm moving all of 'Jake''s associated entities to 'Mike', then removing 'Jake'. Problem is that Doctrine attempts to DELETE 'Jake' first, which makes the constraint fail because 'Robert' is still points to 'Jake'.
What's the proper way of doing this?