dongxu1029 2016-10-15 07:43
浏览 32
已采纳

如何自动将用户对象传递给Symfony 3表单?

I'm learning symphony 3 framework and I ran into a problem that should be basic but I couldn't find a solution yet.

I have an order form, data class is set to the Order object, which is ORM mapped to the according table. I have relations set to User, Product and Payment objects. In my form the user can select product and payment and it would work so far. Only I don't know how to pass the current user object to the form, so it validates. I don't want to put a form field for user in, because it always is the current user object and the user shouldn't be able to see or modify it somehow.

Is there an option besides making the user relation nullable and adding it after form validation and before persisting? Doesn't seem best practice to have this field optional in database.

  • 写回答

1条回答 默认 最新

  • drxt70655 2016-10-15 09:49
    关注

    In this case you don't have to think too difficult. Just set the user entity before persisting the Product entity

    if ($form->isSubmitted() && $form->isValid())
    {
            $em = $this->getDoctrine()->getManager();
    
            $product->setUser($this->getUser());
    
            $em->persist($product);
            $em->flush();
    
            return $this->redirectToRoute('some_route');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器