Doctrine 2 OneToMany Cascade SET NULL [英] Doctrine 2 OneToMany Cascade SET NULL

查看:131
本文介绍了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;
}

干杯!

这篇关于Doctrine 2 OneToMany Cascade SET NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆