教义 2 OneToMany 级联设置 NULL [英] Doctrine 2 OneToMany Cascade SET NULL

查看:15
本文介绍了教义 2 OneToMany 级联设置 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法删除或更新父行:外键约束失败.

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

class Teacher {

    /**
     *@ORMOneToMany(targetEntity="publication", mappedBy="teacher")
     */
    protected $publications;
}

class Publication {

    /**
     * @ORMManyToOne(targetEntity="Teacher", inversedBy="publications")
     * @ORMJoinColumn(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.

我不知道在 Doctrine 中如何做到这一点,这可能吗?还是必须始终与老师建立关系?

I don't know how do that in Doctrine, Is it possible? Or always the relationship has to be with a teacher?

推荐答案

您应该在实体 Publication 的注解中添加选项 onDelete="SET NULL",如下所示:

You should add the option onDelete="SET NULL" in the annotation of your entity Publication like this:

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

干杯!

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

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