Symfony2 Doctrine2 Cascading defined but not working

96 Views Asked by At

I have an entity which has a relationship which contains settings (multiple). To add or delete an entity I have to use cascading, but it isn't working and I can't find the error in my code.

The entity:

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

The error I'm getting:

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`))

Hopefully someone can solve this issue. I'm really stuck at the moment

1

There are 1 best solutions below

1
On BEST ANSWER

cascade settings only appears then you operate with your entites via EntityManager. Then you make a DELETE query it doent care. You need to set onDelete="CASCADE" in DepartmentSettings entity which make a trigger in database.

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