dongwopu8210 2016-12-30 10:30
浏览 15

在持久化+刷新之后,doctrine生成的标识符怎么样?

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));
}
}
  • 写回答

1条回答 默认 最新

  • drake900918 2016-12-30 10:38
    关注

    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...

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭