删除通过一对多关系连接的所有父子实体


in CalcResult entity
    /**
     * @ORMManyToOne(targetEntity="AcmeUserBundleEntityCalcResult",inversedBy="buySellResults",cascade={"persist"})
     * @ORMJoinColumn(name="calcResult_id", referencedColumnName="id")
     */
    private $calcResult;

in BuySellResult entity 
        /**
     * @ORMOneToMany(targetEntity="AcmeUserBundleEntityBuySellResult",mappedBy="calcResult",cascade={"persist"})
     */
    private $buySellResults;

CalcResult行有一些BuySellResult,这两个条目有父子关系。

当我删除一行

时,我想删除与一行CalcResult行连接的所有BuySellResultCalcResult

但是当我尝试删除CalcResult行时,

发生此错误。我该如何解决?

我添加了cascade={"persist"},但我不确定这是实现我目标的正确方法。

[3/4] ForeignKeyConstraintViolationException: An exception occurred while executing 'DELETE FROM CalcResult WHERE id = ?' with params [1004]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`wisdom`.`buysellresult`, CONSTRAINT `FK_C1A2F7B3CFB4BB7` FOREIGN KEY (`calcResult_id`) REFERENCES `CalcResult` (`id`))   +

你需要改变

cascade={"persist"}

cascade={"persist", "remove"}, orphanRemoval=true

更多细节在这里:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#orphan-removal

相关内容

  • 没有找到相关文章

最新更新