Php 删除一家公司的自我参照原则

Php 删除一家公司的自我参照原则,php,symfony,testing,doctrine-orm,doctrine,Php,Symfony,Testing,Doctrine Orm,Doctrine,你好,我对我的symfony项目有问题 我有一个监听器实体 /** * Listener * * @ORM\Entity(repositoryClass="AppBundle\Entity\ListenerRepository") * @ORM\Table("listeners", uniqueConstraints={@ORM\UniqueConstraint(name="sponsor_code", columns={"sponsor_code"})}) * @ORM\HasLif

你好,我对我的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;
此属性是侦听器实体(当前类)的名称

侦听器使用此方法添加到此数组集合中

/**
  * 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
如果我知道发生了什么,他正在试图删除与其他监听器“链接”的监听器,以获得赞助的监听器

我想我需要一个级联删除,但不知道怎么做。如果有人能给我解释,那会很酷

这是listener表

列类型注释 id int(11)自动递增
帐户id int(11)NULL
赞助商id int(11)NULL
站点id int(11)NULL
firstname varchar(100)NULL
性别varchar(255)NULL
出生年份整数(11)空
picture varchar(255)NULL
赞助商代码varchar(6)
在datetime为空时发起的\u
在datetime创建\u

更新了\u at datetime NULL

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

如果需要更多详细信息,请告诉我