[英]Doctrine update entity with arraycollection dont remove items
我有這個課程:
摘要帳單
/**
* @ORM\Table(name="billing_invoice")
* @ORM\Entity(repositoryClass="BillingBundle\Repository\AbstractBillingInvoiceRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="billingInvoiceType", type="string")
* @ORM\DiscriminatorMap({"ProFormaInvoice" = "\BillingBundle\Entity\BillingInvoices\ProFormaInvoice", "Invoice" = "\BillingBundle\Entity\BillingInvoices\Invoice"})
* @GRID\Column(id="billingInvoiceType", filterable=false, type="abstract_billing_invoice")
* @GRID\Column(id="downloadInvoice", filterable=false, type="download_invoice", safe=false)
* @Gedmo\SoftDeleteable()
*/
abstract class AbstractBillingInvoice
{
.....
/**
* @ORM\OneToMany(targetEntity="BillingBundle\Entity\BillingInvoiceItem", mappedBy="billingInvoice", cascade={"persist","remove"}, orphanRemoval=true)
*/
private $billingInvoiceItems;
......
public function __construct()
{
$this->billingInvoiceItems = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getBillingInvoiceItems()
{
return $this->billingInvoiceItems;
}
public function setBillingInvoiceItems($billingInvoiceItems)
{
$this->billingInvoiceItems = $billingInvoiceItems;
return $this;
}
public function addBillingInvoiceItems(BillingInvoiceItem $billingInvoiceItems)
{
$billingInvoiceItems->setBillingInvoice($this);
$this->billingInvoiceItems->add($billingInvoiceItems);
return $this;
}
public function removeBillingInvoiceItems(BillingInvoiceItem $billingInvoiceItems)
{
if ($this->billingInvoiceItems->contains($billingInvoiceItems)) {
$this->billingInvoiceItems->remove($billingInvoiceItems);
}
return $this;
}
和BillingInvoiceItem
class BillingInvoiceItem
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="BillingBundle\Entity\AbstractBillingInvoice", inversedBy="billingInvoiceItems")
* @ORM\JoinColumn(nullable=false, referencedColumnName="id")
* @Assert\NotBlank()
*/
private $billingInvoice;
.....
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function setBillingInvoice($billingInvoice)
{
$this->billingInvoice = $billingInvoice;
return $this;
}
public function getBillingInvoice()
{
return $this->billingInvoice;
}
一切正常,除了一件事-我無法從收藏夾中刪除物品。 添加項目沒有問題,但是如果缺少某些項目,則有兩種情況-如果刪除了ArrayCollection的最后一個元素,則數據庫中沒有任何反應。 如果刪除了其他元素,則會復制最后一個元素而不是它。
如果我在onFlush方法中的EntityListenere中轉儲實體AbstractBillingInvoice,我會看到正確的數據。 如果刪除元素,則元素實際上丟失了,但是在刷新后,此元素又回到了數據庫中。
我做錯了什么?
非常感謝D
通常,我們通過use Doctrine\\Common\\Collections\\ArrayCollection;
通過ArrayCollection
管理許多關系use Doctrine\\Common\\Collections\\ArrayCollection;
包。
您可以這樣更新代碼:
public function __construct() {
$this->billingInvoice = new ArrayCollection();
// [...]
}
public function getBillingInvoice(){
return $this->billingInvoice->toArray();
}
public function addBillingInvoice(BillingInvoice $billingInvoice){
if(!$this->billingInvoice->contains($billingInvoice)){
$this->billingInvoice->add($billingInvoice);
}
}
public function removeBillingInvoice(BillingInvoice $billingInvoice){
if($this->billingInvoice->contains($billingInvoice)){
$this->billingInvoice->removeElement($billingInvoice);
}
}
然后,當您想刪除一個關系時,只需調用:
$myEntity->removeBillingInvoice($myBillingInvoice);
$em->persist($myEntity);
$em->flush();
但是我不確定這是否可以解決您的最初問題,您是否嘗試檢查tou billingInvoice實體是否也刪除了反向映射?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.