![](/img/trans.png)
[英]Symfony2 - Collection of Forms - Update -ManytoMany-Relation
[英]Update collection in relation manytomany in Doctrine with Symfony
我有2個與ManyToMany相關的實體:路由和聯系。
實體數量:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Bdd\Contact", inversedBy="routages", cascade={"persist"})
* @ORM\JoinTable(name="routages_contacts")
*/
private $contacts;
實體聯系人:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Routage\Routage", mappedBy="contacts")
*/
private $routages;
但是,當我向其集合中的一個路由添加一些聯系人時,它將創建X個查詢。 但是我想限制查詢的數量。
這是我的控制器部分:
foreach($Abonnes as $Contact){
$Routage->addContact($Contact);
$this->getDoctrine()->getManager()->persist($Routage);
}
$this->getDoctrine()->getManager()->flush();
線
$this->getDoctrine()->getManager()->persist($Routage);
應該在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.