Tutorial 2 OneToMany Cascade SET NULL

Mistake

Cannot delete or update parent row: foreign key constraint fails.

Classes

class Teacher { /** *@ORM\OneToMany(targetEntity="publication", mappedBy="teacher") */ protected $publications; } class Publication { /** * @ORM\ManyToOne(targetEntity="Teacher", inversedBy="publications") * @ORM\JoinColumn(name="teacher_id", referencedColumnName="id") */ protected $teacher; } 

I want to

I want to make the id_teacher change to NULL when the teacher is deleted. I want to keep the publication, but without reference to the professor.

I do not know how this is done in the Doctrine, is this possible? Or should the relationship always be with the teacher?

+39
set null doctrine one-to-many
Jan 13 '12 at 23:29
source share
1 answer

You must add the onDelete="SET NULL" option to the annotation of your Publish object as follows:

 class Publication { /** * @ORM\ManyToOne(targetEntity="Teacher", inversedBy="publications") * @ORM\JoinColumn(name="teacher_id", referencedColumnName="id", onDelete="SET NULL") */ protected $teacher; } 

Hurrah!

+122
Jul 27 '12 at 15:59
source share



All Articles