具有数组集合的学说更新实体不会删除项目



我有这个类:

抽象账单发票

/**
* @ORMTable(name="billing_invoice")
* @ORMEntity(repositoryClass="BillingBundleRepositoryAbstractBillingInvoiceRepository")
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="billingInvoiceType", type="string")
* @ORMDiscriminatorMap({"ProFormaInvoice" = "BillingBundleEntityBillingInvoicesProFormaInvoice", "Invoice" = "BillingBundleEntityBillingInvoicesInvoice"})
* @GRIDColumn(id="billingInvoiceType", filterable=false, type="abstract_billing_invoice")
* @GRIDColumn(id="downloadInvoice", filterable=false, type="download_invoice", safe=false)
* @GedmoSoftDeleteable()
*/
abstract class AbstractBillingInvoice
{
.....
/**
* @ORMOneToMany(targetEntity="BillingBundleEntityBillingInvoiceItem", 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;
}

和计费发票项

class BillingInvoiceItem
{
/**
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMManyToOne(targetEntity="BillingBundleEntityAbstractBillingInvoice", inversedBy="billingInvoiceItems")
* @ORMJoinColumn(nullable=false, referencedColumnName="id")
* @AssertNotBlank()
*/
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

通常我们通过从包中ArrayCollection来管理许多关系use DoctrineCommonCollectionsArrayCollection;

您可以像这样更新代码:

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 billing发票实体是否也删除了反向映射?

最新更新