Doctrine 2 OneToMany Cascade SET NULL
问题描述:
无法删除或更新父行:外键约束失败。
Cannot delete or update a parent row: a foreign key constraint fails.
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;
}
我想要
我想要的是在删除老师时将id_teacher修改为NULL。我想保留出版物,但不参考教授。
I want
What I want is to make it that when you delete a teacher, the id_teacher is modified to NULL. I want to keep the publication but without reference to Professor.
我不知道在教义中怎么做,是可能吗?或者总是与老师的关系?
I don't know how do that in Doctrine, Is it possible? Or always the relationship has to be with a teacher?
答
您应该添加选项 onDelete = SET NULL
在您的实体的注释中发布如下:
You should add the option onDelete="SET NULL"
in the annotation of your entity Publication like this:
class Publication
{
/**
* @ORM\ManyToOne(targetEntity="Teacher", inversedBy="publications")
* @ORM\JoinColumn(name="teacher_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $teacher;
}
干杯!