oro*_*edd 8 doctrine foreign-keys
我有两个具有一对一单向关系的实体:
class Foo {
...
/**
* @OneToOne(targetEntity="Bar")
*/
private $bar;
...
}
class Bar {
...
}
Run Code Online (Sandbox Code Playgroud)
当我尝试删除Bar实体时,我收到此错误:
完整性约束违规:1451无法删除或更新父行:外键约束失败
如何在不失去删除Bar实体的能力的情况下保持单向关系?
使用Doctrine 2,您需要做的是:
class Foo {
...
/**
* @OneToOne(targetEntity="Bar")
* @JoinColumn(name="bar_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $bar;
...
}
class Bar {
...
}
Run Code Online (Sandbox Code Playgroud)
该onDelete ="瀑布"会做什么卡比在他的回答(设置在级联删除你的外键)说.这样,当您删除Bar实体时,关联的Foo实体也将被删除.
如果你不想删除你的Foo实体,你只需用onDelete ="SET NULL"替换onDelete ="Cascade"即可.
您可以使用“ 孤立删除”。它的工作原理与one-to-one,one-to-many和many-to-many关联。
您只需添加以下orphanRemoval=true选项:
@OneToOne(targetEntity="Bar", orphanRemoval=true)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助某人。
设置外键 ON DELETE 级联(这也会删除 Foo 记录)或 SET NULL(当你删除 Bar 时,这会将列设置为 NULL)
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html