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
行连接的所有BuySellResult
行CalcResult
。
但是当我尝试删除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