gpt4 book ai didi

php - Doctrine Cascade Delete OneToMany 自引用

转载 作者:行者123 更新时间:2023-11-28 21:24:26 26 4
gpt4 key购买 nike

你好,我的 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

如果我明白发生了什么事,他正试图删除与赞助听众的其他听众“链接”的听众。

我想我需要一个级联删除,但不知道该怎么做。如果有人能给我解释一下,那就太好了

这是监听表

列类型注释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
赞助商代码 varchar(6)
sponsored_at datetime NULL
创建日期时间
updated_at datetime NULL

最佳答案

对于类属性,您需要删除设置注释 onDelete="SET NULL"并使其可为空

关于php - Doctrine Cascade Delete OneToMany 自引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com