如何使用 Doctrine 1.2 进行级联删除?
我一直在努力解决 Doctrine ORM 中的定义级联行为。
根据 文档,应该使用onDelete: CASCADE
进行数据库级级联(这就是我在这里试图实现的目标)。
完整的示例可以在 Symfony 教程 中看到。
但是,在我的架构中,所有此类级联规范都被忽略。
以下是相关摘录:
Advancement:
columns:
association_id:
type: integer(4)
notnull: true
task_id:
type: integer(4)
notnull: true
state:
type: enum
values: ['todo', 'current', 'done', 'cancelled']
notnull: true
relations:
Association:
class: Association
local: association_id
foreignType: one
onDelete: CASCADE
Task:
class: Task
local: task_id
foreignType: one
onDelete: CASCADE
当然,任务和关联表的定义是正确的。我不会首先将它们发布在这里,以避免问题太长,但请询问是否有必要。
这是生成的 SQL(我的换行符):
CREATE TABLE advancement
(id INTEGER PRIMARY KEY AUTOINCREMENT,
association_id INTEGER NOT NULL,
task_id INTEGER NOT NULL,
state VARCHAR(255) NOT NULL);
没有 CASCADE
的痕迹(顺便说一下,也没有 REFERENCES
...)。当然,级联不起作用,我必须通过更改默认的后端操作来手动实现它。
有谁知道我在这里做错了什么?
I have been struggling with defining cascade behavior in Doctrine ORM.
According to the documentation, one is supposed to use onDelete: CASCADE
for database-level cascade (which is what I am trying to achieve here).
A complete example may be seen on the Symfony tutorial.
However, all such cascade specifications are ignored in my schema.
Here is the relevant excerpt:
Advancement:
columns:
association_id:
type: integer(4)
notnull: true
task_id:
type: integer(4)
notnull: true
state:
type: enum
values: ['todo', 'current', 'done', 'cancelled']
notnull: true
relations:
Association:
class: Association
local: association_id
foreignType: one
onDelete: CASCADE
Task:
class: Task
local: task_id
foreignType: one
onDelete: CASCADE
Of course, the Task and Association tables are defined correctly. I won't post them here in the first place to avoid a too long question, but please ask if that seems necessary.
Here is the generated SQL (linebreaks mine):
CREATE TABLE advancement
(id INTEGER PRIMARY KEY AUTOINCREMENT,
association_id INTEGER NOT NULL,
task_id INTEGER NOT NULL,
state VARCHAR(255) NOT NULL);
Not a trace of a CASCADE
(nor a REFERENCES
, by the way…). Of course, cascading does not work, and I had to implement it manually by altering the default backend actions.
Does anyone know what I am doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为如果你的 RDBMS(是 MySQL 吗?)不支持级联,学说就会闭嘴。如果您将 MySQL 与 MyISAM 存储引擎一起使用(顺便说一句,它也不支持 REFERENCES ......),就会出现这种情况。如果我是你,我会尝试直接使用 RDBMS 客户端创建级联,只是为了检查是否可行。
I think doctrine will shut up about the fact that your RDBMS (is it MySQL?) does not support cascades if it doesn't. This is the case if you use MySQL with the MyISAM storage engine (which does not support
REFERENCES
either, by the way...). If I were you, I'd try to create a cascade directly with your RDBMS client, just to check whether it is possible or not.