您好,我的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;
此属性是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
如果我了解正在发生的事情,他正在尝试删除与SponsoredListeners的其他监听器“链接”在一起的监听器。
我认为我需要一个级联删除,但不确定如何执行。如果有人可以解释我,那可能真的很酷
这是监听器表
列类型注释
id int(11)自动递增
account_id int(11)NULL
Sponsor_id int(11)NULL
station_id int(11)NULL
名字varchar(100)NULL
性别varchar(255)NULL
出生年份int(11)NULL
图片varchar(255)NULL
Sponsor_code varchar(6)
Sponsored_at datetime NULL
created_at datetime
datetime中的updated_at NULL
最佳答案
对于类属性,您需要删除set注释onDelete =“ SET NULL”并将其设置为可空
关于php - Doctrine 级联删除一对多自引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44160922/