![](/img/trans.png)
[英]Proper way to update bidirectional Many to Many relationship symfony2-doctrine
[英]Best way to update entity one to many relationship in Symfony 2 / Doctrine 2
在 doctine 中更新实体一对多关系的最佳方法是什么? 例如:我有一个名为 booking 的实体,它映射了一对多的 Guest 实体。
编辑预订时,可以通过添加或删除客人来更改客人的数量。 目前,我统计提交的客人数量,如果与当前客人数量不同,我删除预订客人并重新添加新客人
对我来说,这似乎不对,我想知道其他人如何处理这个问题。
代码示例:
if (count($collection)) {
$numberGuests = count($this->getEntity()->getGuests());
foreach ($collection as $guest) {
if ($numberGuests != count($guests)) {
// delete guest if the number has changed
$this->getGuestManager()->delete($guest);
} else {
// update entity
$guest->setArrayData(Tools::getData($i, $guests));
}
}
}
我认为没有最好的方法,但是您现在的方法不正确。 为什么如果用户修改了客人,但人数仍然相同(他从列表中删除了一位客人,但添加了一位新客人)?
无论如何,我认为每次有人编辑时“重置”关系并不是一个糟糕的方法(尽管可能不是最有效的),您只需要从拥有方(客人)设置预订。 (在多对一关系中,“多”方必须是拥有方)
我会在控制器中这样做:
if ($editForm->isValid()) {
//find all guests entities that has this booking related
$oldguests=$em->getRepository('YourBundle:Guest')->findby(array("booking"=>$entity));
//well you will need to custom a little bit better this "findby"
foreach($oldguest as $guest){
//remove the booking for that guest. So that guest won't have any booking linked
$guest->removeBooking();
$em->persist($guest);
}
//now we make the link with guest and booking
//$form->submit($request) should have set the current entity with the current guests the user selected
foreach($entity->getGuests() as $currentguest){
$currentguest->setBooking($entity);
$em->persist($guest);
}
$em->persist($entity);
$em->flush();
}
在来宾实体中,我将添加 function removeUser
//guest.php
public function removeBooking(){
$this->booking=NULL;
}
Maybe is more elegant if you create a function in the GuestRepository.php that does what the controller is doing, and in the controller you just call that function.
你真的需要照顾关系的拥有方。 如果您通过拥有方允许版本,即使用预订更新客人,则默认代码应用程序/控制台为您提供根本不需要任何类型的自定义:两个实体都将正确更新。
为了简单起见,我们可以说:让用户更新关系的拥有方 => 一切都是自动的。 让用户更新关系的反面 => 需要手动定制。 (这对于多对多关系也是一样的)。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.