简体   繁体   English

学说级联删除一对多自引用

[英]Doctrine Cascade Delete OneToMany self Referencing

Hello i have an issue with my symfony project 您好,我的symfony专案有问题

I have a listener entity 我有一个侦听器实体

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

in this class there is a sponsoredListeners property 在此类中,有一个SponsoredListeners属性

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

this property is an ArrayCollection of Listener entities (the current class) 此属性是Listener实体的ArrayCollection (当前类)

listeners are added in this array collection using this method 使用此方法将侦听器添加到此数组集合中

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

There problem is that when i try to delete all listeners from my listener tables during my testing i get these errors 问题是,当我在测试期间尝试从侦听器表中删除所有侦听器时,出现这些错误

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

if i understand what is happening he is trying to delete listeners with are "linked" with other listeners for sponsoredListeners. 如果我了解发生了什么,他正尝试删除与SponsoredListeners的其他监听器“链接”的监听器。

I think i need a cascade delete but not sure how to do it. 我认为我需要一个级联删除,但不确定如何执行。 If someone can explain me it could be really cool 如果有人可以解释我,那可能真的很酷

here is the listener table 这是监听器表

Column Type Comment id int(11) Auto Increment 列类型注释id int(11)自动递增
account_id int(11) NULL account_id int(11)NULL
sponsor_id int(11) NULL Sponsor_id int(11)NULL
station_id int(11) NULL station_id int(11)NULL
firstname varchar(100) NULL 名字varchar(100)NULL
gender varchar(255) NULL 性别varchar(255)NULL
birthyear int(11) NULL 出生年份int(11)NULL
picture varchar(255) NULL 图片varchar(255)NULL
sponsor_code varchar(6) Sponsor_code varchar(6)
sponsored_at datetime NULL Sponsored_at datetime NULL
created_at datetime created_at datetime
updated_at datetime NULL datetime的updated_at NULL

对于类属性,您需要删除set注释onDelete =“ SET NULL”并使其可为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM