[英]Update collection in relation manytomany in Doctrine with Symfony
I have 2 entities related with ManyToMany : Routage and Contact. 我有2个与ManyToMany相关的实体:路由和联系。
Entity Routage : 实体数量:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Bdd\Contact", inversedBy="routages", cascade={"persist"})
* @ORM\JoinTable(name="routages_contacts")
*/
private $contacts;
Entity Contact : 实体联系人:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Routage\Routage", mappedBy="contacts")
*/
private $routages;
But when I add some Contact to one Routage in his Collection, it creates X queries. 但是,当我向其集合中的一个路由添加一些联系人时,它将创建X个查询。 But I want limit the number of queries. 但是我想限制查询的数量。
Here my controller part : 这是我的控制器部分:
foreach($Abonnes as $Contact){
$Routage->addContact($Contact);
$this->getDoctrine()->getManager()->persist($Routage);
}
$this->getDoctrine()->getManager()->flush();
the line 线
$this->getDoctrine()->getManager()->persist($Routage);
should be out of the foreach loop so your code should look like this : 应该在foreach循环之外,因此您的代码应如下所示:
foreach($Abonnes as $Contact){
$Routage->addContact($Contact);
}
$this->getDoctrine()->getManager()->persist($Routage);
$this->getDoctrine()->getManager()->flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.