Reputation: 1085
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
Upvotes: 1
Views: 86
Reputation: 12033
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;
Upvotes: 3