原则 2 OneToMany 级联 SET NULL

发布于 2024-12-27 02:31:46 字数 529 浏览 1 评论 0原文

错误

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

课程

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。我想保留该出版物,但不提及教授。

我不知道在教义中如何做到这一点,可能吗?或者总是与老师有关系?

The error

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

The 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那一片橙海, 2025-01-03 02:31:46

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

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

此修改在表声明本身中注册。因此,您需要进行数据库迁移或架构更改才能使其生效。

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;
}

This modification is registered in the table declaration itself. Hence, you need a database migration or a schema change for this to take effect.

微暖i 2025-01-03 02:31:46

对于 Symfony 6 (PHP 8)(引擎“InnoDB”)

#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]

For Symfony 6 (PHP 8) (Engine 'InnoDB')

#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
三生殊途 2025-01-03 02:31:46

老问题,所以这适合任何寻找解决方案的人。

如果您不想对数据库架构进行任何更改,您可以使用学说事件系统:

use Doctrine\ORM\Mapping as ORM;

...

#[ORM\PreRemove]
public function clearPublications(): void
{
    foreach ($this->getPublications() as $publication) {
        $publication->setTeacher(null);
    }
}

Old question, so this is for anyone searching for solution.

In case you don't want to make any changes to database schema you can make use of doctrine event system:

use Doctrine\ORM\Mapping as ORM;

...

#[ORM\PreRemove]
public function clearPublications(): void
{
    foreach ($this->getPublications() as $publication) {
        $publication->setTeacher(null);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文