究竟如何使用onDelete =“SET NULL” - Doctrine2

时间:2021-09-12 07:07:08

I have a class Category containing this:

我有一个类别包含这个:

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

and a class Friend with 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?

我尝试将undele =“CASCADE”放到ManyToOne注释中,然后到OneToMany,我尝试了上面显示的内容,我尝试在OneToMany注释中使用cascade = {“remove”},但没有任何效果!我也找不到一个例子。你可以帮我吗?

1 个解决方案

#1


49  

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上工作。

#1


49  

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上工作。