如何在Symfony2 Doctrine中使用级联选项? [英] How do I use the cascade option in Symfony2 Doctrine?

查看:149
本文介绍了如何在Symfony2 Doctrine中使用级联选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 Symfony2 中的原则中的rel-nofollow noreferrer> 级联 选项>。

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个实体:

报告

/**
* @ORM\OneToMany(targetEntity="Response", mappedBy="report")
*/
protected $responses;

/**
* @ORM\OneToMany(targetEntity="Response", mappedBy="report")
*/
protected $sms;

响应

/**
 * @ORM\ManyToOne(targetEntity="Report", inversedBy="responses")
 */
protected $report;

短信

/**
 * @ORM\ManyToOne(targetEntity="Report")
 */
protected $report;

现在我想删除一个响应实体,但我得到

Now I would like to delete a Response entity but I get


SQLSTATE [23000]:完整性约束违反:1451无法删除或更新父行:

a外键约束失败( mybundle sms ,CONSTRAINT FK_B0A93A77BB333E0D FOREIGN KEY( reportId )参考报告 id ) )

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row:
a foreign key constraint fails (mybundle.sms, CONSTRAINT FK_B0A93A77BB333E0D FOREIGN KEY (reportId) REFERENCES report (id))

我在哪里可以使用级联选项,我应该使用哪个选项(分离删除)?

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.

推荐答案

尝试使用

/**
 * @ORM\ManyToOne(targetEntity="Report", inversedBy="responses")
 * @ORM\JoinColumn(name="reportId", referencedColumnName="id", onDelete="CASCADE")
 */
protected $report;

然后更新yor模式。它将添加数据库级别级联

And then update yor schema. It will add database level Cascading

这篇关于如何在Symfony2 Doctrine中使用级联选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆