Symfony2 Doctrine2级联定义但不起作用

时间:2013-12-04 11:16:40

标签: symfony doctrine-orm cascade

我有一个实体,其关系包含设置(多个)。要添加或删除实体,我必须使用级联,但它不起作用,我在代码中找不到错误。

实体:

/**
 * @var DepartmentSettings
 *
 * @ORM\OneToMany(targetEntity="DepartmentSettings", mappedBy="department", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
 */
protected $departmentsettings;

我得到的错误:

An exception occurred while executing 'DELETE FROM department WHERE id = ?' with params [13]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`hotflo20`.`department_settings`, CONSTRAINT `FK_DCFBBAEFAE80F5DF` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`))

希望有人可以解决这个问题。我现在真的被困住了

1 个答案:

答案 0 :(得分:3)

只有

cascade设置出现,然后通过EntityManager与您的entites一起操作。然后你做一个DELETE查询它非常小心。您需要在onDelete="CASCADE"实体中设置DepartmentSettings,以便在数据库中生成触发器。

/**
 * @ORM\ManyToOne(targetEntity="Department", inversedBy="departmentsettings")
 * @ORM\JoinColumn(name="department_id", referencedColumnName="id", onDelete="CASCADE")
 */
protected $department;