2017-05-24 40 views
0

你好,我有一個問題,我的symfony項目主義級聯刪除一對多自參考

我有一個聽衆實體

/** 
* 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 
{ 
    ... 
} 

在這個類中有一個sponsoredListeners財產

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

這屬性是聽衆實體(當前類)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 

如果我沒有理解他正試圖刪除監聽器的情況與其他監聽器「鏈接」爲sponsorListeners。

我想我需要一個級聯刪除,但不知道如何去做。如果有人能解釋我可能真的很酷

這裏是監聽器表

列類型註釋 ID INT(11)自動遞增
ACCOUNT_ID INT(11)NULL
sponsor_id INT(11)NULL
station_id INT(11)NULL
姓名VARCHAR(100)NULL
性別VARCHAR(255)NULL
birthyear INT(11)NULL
圖象VARCHAR(255)NULL
sponsor_code VARCHAR(6)
sponsored_at的datetime NULL
created_at日期時間
的updated_at的datetime NULL

+0

如果一個類的屬性,你需要更多的細節只是告訴我 – RomMer

回答

1

對於您需要刪除組註釋onDelete = 「SET NULL」,並使其可爲空

+0

謝謝你剛發現它仍然接受你的答案:) – RomMer