Reputation: 883
I don't understand a small part of the doctrine documentation. Part of the documentation :
Generated entity identifiers / primary keys are guaranteed to be available after the next successful flush operation that involves the entity in question. You can not rely on a generated identifier to be available directly after invoking persist. The inverse is also true. You can not rely on a generated identifier being not available after a failed flush operation.
Does it mean that if I create an entity which has an auto-generated ID and persist
+flush
that entity I can't send the id to an other controller ?
Somethink like :
public function testAction()
{
$fabulous = new User();
$fabulousForm = $this->get('form.factory')->create(FabulousType::class, $fabulous);
if($fabulousForm->isSubmitted() && $fabulousForm->isValid())
{
$fabulousId = $fabulousForm->getData()->getUser()->getId();
$em = $this->getDoctrine()->getManager()->persist($fabulous)->flush();
return $this->redirectToRoute('my_fabulous_route', array('user_id' => fabulousId));
}
}
Upvotes: 0
Views: 637
Reputation: 20201
You can not rely on a generated identifier being not available after a failed flush operation.
So, not really, if your flush
has completed successfully, your ID will be available and can be used safely.
One exception to this is when transaction is being used. If I am not mistaken, the flush
will also create a valid ID, but failing to commit a transaction properly results in record being removed.
Hope this helps...
Upvotes: 1