2017-05-24 9 views
0

こんにちは私はsponsoredListenersプロパティ教義カスケード参照OneToMany自己を削除

/** 
    * @Groups({"listener_sponsored"}) 
    * 
    * @ORM\OneToMany(targetEntity="Listener", mappedBy="sponsor") 
    */ 
    private $sponsoredListeners; 

これがある

私は、このクラスでリスナーエンティティ

/** 
* Listener 
* 
* @ORM\Entity(repositoryClass="AppBundle\Entity\ListenerRepository") 
* @ORM\Table("listeners", uniqueConstraints={@ORM\UniqueConstraint(name="sponsor_code", columns={"sponsor_code"})}) 
* @ORM\HasLifecycleCallbacks() 
*/ 
class Listener implements ListenerInterface 
{ 
    ... 
} 

を持っている私のsymfonyプロジェクトの問題を持っていますプロパティはListenerエンティティ(現在のクラス)のArrayCollectionです

リスナーはこの方法

/** 
    * Add sponsored Listener 
    * 
    * @param \AppBundle\Entity\Listener $sponsoredListener 
    * 
    * @return Listener 
    */ 
    public function addSponsoredListener(\AppBundle\Entity\Listener $sponsoredListener) 
    { 
     if (!$this->sponsoredListeners->contains($sponsoredListener)) { 
      $this->sponsoredListeners[] = $sponsoredListener; 
     } 

     $sponsoredListener->setSponsor($this); // just set the sponsor property for the listener given in parameters of this function (addSponsoredListener)  
     return $this; 
    } 

を使用して、この配列のコレクションに追加されたそこの問題は、私は私のテスト中に私のリスナーテーブルからすべてのリスナーを削除しようとすると、私はこれらのエラーを得ることである

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`myradio_test`.`listeners`, CONSTRAINT `FK_CEFB12DB12F7FB51` FOREIGN KEY (`sponsor_id`) REFERENCES `listeners` (`id`)) 

/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:60 
/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:128 
/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:996 
/var/www/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php:149 
/var/www/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:136 
/var/www/vendor/liip/functional-test-bundle/Test/WebTestCase.php:451 
/var/www/src/ApiBundle/Tests/Controller/ArtistsControllerTest.php:16 
/var/www/src/ApiBundle/Tests/Controller/ArtistsControllerTest.php:27 

私は理解している場合彼はsponsoredListenerのための他のリスナーと "リンク"しているリスナーを削除しようとしています。

私はカスケード削除が必要だと思いますが、それを行う方法がわかりません。誰かが私に説明できるなら、それは本当にクール

ここリスナー表

列タイプコメント IDのint(11)自動インクリメント
ACCOUNT_ID int型(11)NULL
sponsor_id int型(11)NULLがあるかもしれません
station_idはint(11)NULL
FIRSTNAMEのVARCHAR(100)NULL
性別VARCHAR(255)NULL
birthyearはint(11)NULL
ピクチャVARCHAR(255)NULL
日時NULL updated_atのsponsor_codeのVARCHAR(6)
sponsored_at日時NULL
のcreated_at datetime型

+0

それがNULL可能にする必要があり、クラスのプロパティのためにより多くの詳細が必要なだけ私に教えてください – RomMer

答えて

1

あなたがセット注釈onDelete = "SETのNULL" を削除した場合

+0

あなたはまだそれがあなたの答えを受け入れることを発見してくれてありがとう:) – RomMer