是否有任何解決方案,自動做到這一點?更新許多-to-many關聯doctrine2
我的兩個實體:
class User
{
/* *
* @ManyToMany(targetEntity="Product", inversedBy="users")
* @JoinTable(name="user_product",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="idUser")},
* inverseJoinColumns={@JoinColumn(name="product_id", referencedColumnName="idProduct")}
*
*)
*/
protected $products;
}
class Product {
/**
* @ManyToMany(targetEntity="User", mappedBy="products")
*/
protected $users;
}
用戶實體有兩個產品存在已經相關ID(1
,2
):
$user = $entityManager->find('User', 1);
此數組來自視圖與新產品要插入,刪除的數據或已經在列表中的任何內容:
$array = array(1, 3, 4);
在這種情況下:
1 = Already in association with User (do nothing)
2 = not in array and should be deleted
3 = should be inserted
4 = should be inserted
如何做到這一點的doctrine2?有沒有一個合併功能自動執行或手動執行?
**答案**:請參考我在http://stackoverflow.com/a/35445060/2423563 – SudarP