doctrine-orm - 使用循环外键删除

标签 doctrine-orm

我有两个相互引用的实体:一个 Page 实体,它引用了 defaultUrl 和一个 Url 实体,它有一个对页面的引用。

这是周期性的,因此一旦我添加了相互引用的页面和网址,我就无法删除它们。

我可以看到两种方法来修复它,但不确定“教义”的方法。

  1. 仅将其中一个关系设为索引,而不设为外键约束
  2. 删除时,关闭外键约束检查

我不知道如何做其中任何一个......你知道吗?

谢谢!

最佳答案

这是设置此功能的 Doctrine 方法。如前所述,您需要将至少一个外键设置为可为空。

还可以考虑将 onDelete 级联设置为 SET NULL。这将使删除过程变得更加简单——在删除记录之前,您不必将键更新为 NULL。

您的 schema.yml 可能如下所示:

Url:
  columns:
    page_id: { type: integer, notnull: true }
  relations:
    Page: { local: page_id, foreign: id }

Page:
  columns:
    default_url_id: { type: integer, notnull: false }  # ALLOWS NULL foreign key here
  relations:
    DefaultUrl: { class: Url, local: default_url_id, foreign: id, onDelete: SET NULL }

关于doctrine-orm - 使用循环外键删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116857/

相关文章:

php - Doctrine 2 在 MVC ZF2 环境中扮演什么角色?

doctrine-orm - Symfony3 - 添加记录 Doctrine 查询的记录器

database - 在没有任何过滤器的情况下搜索实体的原则

doctrine-orm - Stof\DoctrineExtensionsBundle : missing identifier/primary key for Doctrine2

php - Doctrine 2 多级 OneToOne 级联

join - Doctrine 2 中的自动 JOIN 生成

php - AJAX 提交表单的 Symfony2 Controller 访问错误

php - codeigniter 2 与原则 2 - 我可以将数据插入到表中,但无法取回数据

php - 如何使用 Doctrine 2 截断表格?

php - Doctrine Inheritance - 从 Joined table 继承 Single_Table