2012-10-15 50 views
5

您好我有下面的類學說2級聯= {「」去掉「}似乎並不奏效

/** 
* MP\User\RegistrationBundle\Entity 
*/ 
namespace MP\User\RegistrationBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Persistence\PersistentObject; 
use MP\Services\SiteAdapterBundle\Util\String; 
/** 
* @ORM\Table(name="customer") 
* @ORM\Entity(repositoryClass="MP\User\RegistrationBundle\Repositories\CustomerRepository") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Customer extends PersistentObject 
{ 

    /** 
    * @var string $id 
    * @ORM\Id 
    * @ORM\Column(name="icustomer_id", type="integer") 
    */ 
    protected $id; 

    /** 
    * @var string $addresses 
    * @ORM\OneToMany(targetEntity="MP\User\RegistrationBundle\Entity\Address", mappedBy="customer", cascade={"remove"}) 
    */ 
    protected $addresses; 

以下關係

/** 
* MP\User\RegistrationBundle\Entity 
*/ 
namespace MP\User\RegistrationBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Persistence\PersistentObject; 

/** 
* @ORM\Table(name="custdeladd") 
* @ORM\Entity(repositoryClass="MP\User\RegistrationBundle\Repositories\AddressRepository") 
*/ 
class Address extends PersistentObject 
{ 
     /** 
     * @var integer $suffix 
     * @ORM\Column(name="isuffix", type="integer") 
     * @ORM\Id 
     */ 
     protected $suffix; 

     /** 
     * @var object $customer 
     * @ORM\ManyToOne(targetEntity="MP\User\RegistrationBundle\Entity\Customer", inversedBy="addresses", cascade={"persist"}) 
     * @ORM\JoinColumn(name="icustomer_id", referencedColumnName="icustomer_id") 
     */ 
     protected $customer; 
} 

有誰知道爲什麼當客戶被刪除的地址不是?非常感謝

+0

cascade = {「remove」}爲我工作 – jmoz

回答

28

您的關係定義似乎很好。客戶被刪除的方式是什麼?我的意思是Doctrine沒有在數據庫中直接設置「ON DELETE CASCADE」,所以,如果喲您以「教條」之外的方式移除客戶實體,評論不會被刪除。

你可能會告訴學說直接在數據庫中設置此,在註釋中加入:

@ORM\JoinColumn(name="icustomer_id", referencedColumnName="icustomer_id", onDelete="CASCADE") 

但是,如果你想刪除右鍵學說方式螞蟻這仍然沒有工作單位,嘗試添加「orphanRemoval」爲真,它應該有所幫助:

// Customer.php 
/** 
* @var string $addresses 
* @ORM\OneToMany(targetEntity="MP\User\RegistrationBundle\Entity\Address", mappedBy="customer", cascade={"remove"}, orphanRemoval=true) 
*/ 
protected $addresses; 
+1

非常感謝,mappedBy =「customer」,cascade = {「remove」},OrphanRemoval =真正的OneToMany跳汰機取得了訣竅,我相信我之前嘗試過,但.. – Richard

5

我有一個相當多的麻煩得到這個工作。下面是一些點,可以幫助那些有類似的煩惱:

  1. 所屬的實體需要@OneToMany(... cascade={"remove"}cascade={"all"})
  2. 子實體還需要@JoinColumn(... onDelete="CASCADE")
  3. 另外,如果要修改onDelete="CASCADE"部分,我相信你會需要在更改生效之前更新您的模式。