Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

like image 452
Faery Avatar asked Sep 18 '12 06:09

Faery


1 Answers

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

like image 66
Zeljko Avatar answered Sep 20 '22 04:09

Zeljko