使用 Doctrine2 创建新条目而不是更新旧条目
现在我将 Doctrine2 与 Symfony2 结合使用。我有一个名为“OrderRequest”的实体,其中包含对另一个名为“OrderRequestPerson”的条目类型的两个引用(这些是订购者和接收者字段)。在我的表单中,用户可以选择设置 orderer = 接收者或填写接收者信息。
在这种特定情况下,我有一个错误:订单放置在 orderer = 接收者的位置,因此在数据库中,orderer 和接收者字段具有相同的值(在本例中为 15,指的是 OrderRequestPerson id)。如果管理员将订单更新到 orderer !=receiver 的位置,我需要更新receiver 字段。目前,实体管理器将更新 ID 为 15 的 OrderRequestPerson,从而更新排序者和接收者。有没有办法告诉 Doctrine2 / Symfony2 使用新的 id 为接收者创建一个新条目,并将信息放入该条目中,而不是更新订购者信息?
我的代码发布如下:
// Get the entity manager.
$em = $this->getDoctrine()->getEntityManager();
// Grab the order.
$order = $em->getRepository('OrderRequestBundle:OrderRequest')
->getOrder($id);
// Get important information.
$request = $this->getRequest();
// Create the OrderRequest form.
$form = $this->get('form.factory')->create(new OrderRequestType(), $order);
// Fill the form with the request information.
$form->bindRequest($request);
// Save the information.
$em->flush();
Right now I'm using Doctrine2 in conjunction with Symfony2. I have an entity called "OrderRequest" with contains two references to another entry type called "OrderRequestPerson" (these are the orderer and receiver fields). In my form the user has the option to set the orderer = receiver or to fill out the receiver information.
I have a bug in this specific case: an order is placed where orderer = receiver, thus in the database the orderer and receiver fields have the same value (15 in this case, referring to the OrderRequestPerson id). If an admin goes and updates the order to where the orderer != receiver, I need to update the receiver field. Currently the entity manager will update the OrderRequestPerson with the id of 15, which consequently updates both the orderer and receiver. Is there a way I can tell Doctrine2 / Symfony2 to create a new entry for the receiver, with a new id and place the information into that entry rather than updating the orderer information?
My code is posted below:
// Get the entity manager.
$em = $this->getDoctrine()->getEntityManager();
// Grab the order.
$order = $em->getRepository('OrderRequestBundle:OrderRequest')
->getOrder($id);
// Get important information.
$request = $this->getRequest();
// Create the OrderRequest form.
$form = $this->get('form.factory')->create(new OrderRequestType(), $order);
// Fill the form with the request information.
$form->bindRequest($request);
// Save the information.
$em->flush();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上必须将实体从实体管理器中分离出来,然后再次保留它,这将迫使它作为新实体插入。
现在的问题是,只有当表单要独立修改其中一个或另一个时,您才应该这样做。
我想您可以在绑定表单之前始终执行此操作,然后如果您看到接收器和订购者都包含相同的数据,则丢弃接收器(不要忘记将其从
$em 中删除或至少分离
),并再次将排序者设置为接收者。我希望这是有道理的。但要小心,否则我认为您的数据库中最终会出现大量重复和孤立的数据。
您可以阅读更多内容 在原则文档中分离。
You basically have to detach the entity from the entity manager and then persist it again, which will force it to be inserted as a new one.
Now the problem is, you should only do that if the form is going to modify one or the other independently.
I guess you could do it always before binding the form, and then if you see that both receiver and orderer contain the same data, you discard the receiver (don't forget to remove or at least detach it from the
$em
), and set the orderer as the receiver again.I hope this makes sense. Be careful though otherwise you'll end up with tons of duplicated and orphaned data in your db I think.
You can read more on detach in the doctrine docs.