2012-10-15 9 views
5

Witam mam następujące klasyDoctrine 2 kaskady = { „” usuń "} nie wydaje się działać

/** 
* 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; 

Z poniższej relacji

/** 
* 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; 
} 

Czy ktoś wie dlaczego, gdy klient zostanie usunięty adresy nie są? Wielkie dzięki

+0

kaskada = { „usunąć”} pracował dla mnie – jmoz

Odpowiedz

28

Twoja definicja relacji wydaje się być w porządku.Jaki jest sposób, w jaki klient jest usunięty? Mam na myśli Doctrine nie ustawia "NA KASKU DELETE" bezpośrednio w bazie danych. Jeśli ty usuniesz encję klienta w inny sposób niż "doktryną", komentarze nie zostaną usunięte.

Można powiedzieć doktrynę ustawić to bezpośrednio w bazie danych, dodając w adnotacji:

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

Ale jeśli próbujesz usunąć podmiot w prawym doktryny sposób mrówka to nadal nie działa, spróbuj dodaj "orphanRemoval" do wartości true, powinno to pomóc:

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

Wielkie dzięki, na mappedBy = „Klient”, kaskada = { „usunąć”}, orphanRemoval = true giga na OneToMany załatwiło sprawę, jestem pewien, że próbowałem tego wcześniej, ale .. – Richard

5

Miałem sporo problemów z uruchomieniem tego. Oto kilka punktów, które mogłyby pomóc osobom o podobnych kłopotach:

  1. Podmiot posiadający potrzebuje @OneToMany(... cascade={"remove"} lub cascade={"all"})
  2. Podmiot dziecko potrzebuje również @JoinColumn(... onDelete="CASCADE")
  3. Ponadto, jeśli są modyfikując onDelete="CASCADE" część wierzę trzeba będzie zaktualizować swój schemat, zanim zmiany zaczną obowiązywać.
Powiązane problemy