php 如何准确地使用 onDelete = "SET NULL" - Doctrine2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12471715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:37:35  来源:igfitidea点击:

How exactly to use onDelete = "SET NULL" - Doctrine2

phpdoctrine-orm

提问by Faery

I have a class Categorycontaining this:

我有一个包含这个的类Category

/**
 * @ORM\OneToMany(targetEntity="Friend", mappedBy="category")
 * @ORM\OrderBy({"name" = "ASC"})
 */
protected $friends;

and a class Friendwith this:

和一个班级的朋友

 /**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="friends")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id",  onDelete="SET NULL")
 */
protected $category;

What I want is to be able to delete categories no matter if there are some friends from this category, and if there are - the category field for this friends to be set to NULL.

我想要的是能够删除类别,无论是否有来自该类别的一些朋友,如果有 - 此朋友的类别字段设置为NULL。

I tried to put onDelete="CASCADE"to the ManyToOne annotation, then to the OneToMany, I tried what is shown above, I tried using cascade={"remove"}in the OneToMany annotation, and nothing worked! I couldn't find a example as well. Could you please help me?

我尝试放入onDelete="CASCADE"ManyToOne 注释,然后放入OneToMany,我尝试了上面显示的内容,我尝试cascade={"remove"}在 OneToMany 注释中使用,但没有任何效果!我也找不到例子。请你帮助我好吗?

回答by Zeljko

This must be a crazy answer but did you update database schema? onDelete="SET NULL"is on database level, it must work on innoDB.

这一定是一个疯狂的答案,但您是否更新了数据库架构?onDelete="SET NULL"在数据库级别,它必须在 innoDB 上工作。