如何在 Symfony2 Doctrine 中使用级联选项? [英] How do I use the cascade option in Symfony2 Doctrine?
问题描述
我正在尝试理解 cascade
选项在 Symfony2 的 Doctrine 中.
I'm trying to understand the cascade
option in Doctrine in Symfony2.
我希望能够删除子实体(并且不会触发外键约束错误.)
I would like to be able to delete a child entity (and not trigger the foreign key constraint error.)
我有 3 个实体:
举报
/**
* @ORMOneToMany(targetEntity="Response", mappedBy="report")
*/
protected $responses;
/**
* @ORMOneToMany(targetEntity="Response", mappedBy="report")
*/
protected $sms;
回应
/**
* @ORMManyToOne(targetEntity="Report", inversedBy="responses")
*/
protected $report;
短信
/**
* @ORMManyToOne(targetEntity="Report")
*/
protected $report;
现在我想删除一个 Response
实体,但我得到了
Now I would like to delete a Response
entity but I get
SQLSTATE[23000]:违反完整性约束:1451 无法删除或更新父行:
外键约束失败 (mybundle
.sms
, CONSTRAINT FK_B0A93A77BB333E0D
FOREIGN KEY (reportId
) REFERENCES report
(id
))
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row:
a foreign key constraint fails (mybundle
.sms
, CONSTRAINTFK_B0A93A77BB333E0D
FOREIGN KEY (reportId
) REFERENCESreport
(id
))
在哪里使用 cascade
选项以及应该使用哪个选项(detach
或 remove
)?
Where do I use the cascade
option and which option should I use (detach
or remove
)?
我可以做很多试验和错误来解决这个问题,但我希望得到专家的解释,所以我不会忽略一些事情.
I can do a lot of trial and error to figure this out, but I was hoping for an expert explanation, so I don't overlook something.
推荐答案
尝试使用
/**
* @ORMManyToOne(targetEntity="Report", inversedBy="responses")
* @ORMJoinColumn(name="reportId", referencedColumnName="id", onDelete="CASCADE")
*/
protected $report;
然后更新你的架构.它将添加数据库级级联
And then update yor schema. It will add database level Cascading
这篇关于如何在 Symfony2 Doctrine 中使用级联选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!