Reputation: 57
I have 3 tables. "Dish". Connected with DishTypeAssoc (1..n). And DishTypeAssoc is connected with DishTypeOptionAssoc (1..n).
When I delete "Dish" from controller I got well working cascade deleting. When I delete Dish also DishTypeAssoc and DishTypeOptionAssoc is deleted.
$em->remove($dish);
$em->flush();
It is working fine.
But when I try to delete only DishTypeAssoc with corresponding DishTypeOptionAssoc using Doctrine,
I am using this function in DishTypeAssocRepository
public function removeTypeAssoc($id = null)
{
$q = $this->createQueryBuilder('d')
->delete()
->where('d.dish = :id')
->setParameter('id', $id)
->getQuery()
->execute();
return $q;
}
I got an error:
An exception occurred while executing 'DELETE FROM r_dish_type_assoc WHERE dish_id = ?' with params [3]:
Cannot delete or update a parent row: a foreign key constraint fails (preko_db7
.r_dish_type_option_assoc
, CONSTRAINT FK_4912176D382FD94
FOREIGN KEY (dish_type_assoc_id
) REFERENCES r_dish_type_assoc
(id
))
This is my code of 3 Entities. Dish. DishTypeAssoc. And DishTypeOptionAssoc
class Dish
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="DishTypeAssoc", mappedBy="dish", cascade={"remove"})
*/
private $typeAssoc;
public function __construct()
{
$this->typeAssoc = new ArrayCollection();
}
// ...setters - getters()
}
class DishTypeAssoc
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Dish", inversedBy="typeAssoc", cascade={"persist"})
* @ORM\JoinColumn(name="dish_id",referencedColumnName="id", nullable=true)
*/
private $dish;
/**
* @ORM\OneToMany(targetEntity="DishTypeOptionAssoc", mappedBy="dishTypeOption", cascade={"remove"}) //, orphanRemoval=true)
*/
private $optionAssoc;
public function __construct()
{
$this->optionAssoc = new ArrayCollection();
}
// ...setters - getters()
}
class DishTypeOptionAssoc
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="DishTypeAssoc", inversedBy="optionAssoc")
* @ORM\JoinColumn(name="dish_type_assoc_id", referencedColumnName="id", nullable=true, onDelete = "SET NULL")
*/
private $dishTypeOption;
// ...setters - getters()
}
I dont know why Dish removing works well, and it deletes corresponding DishTypeAssoc and DishTypeOptionAssoc.
But removing only DishTypeAssoc and corresponding DishTypeOptionAssoc doesnt work.
Please, does anyone know why it returns an error ?
Upvotes: 1
Views: 6619
Reputation: 5663
Removing a Dish entity is working thanks to OrphanRemoval and removing cascade to completely delete the DishTypeAssoc and DishTypeOptionAssoc.
However, to delete a DishTypeAssoc, it has to be removed from the ArrayCollection Dish->typeAssoc
You need to remove it explicitly from the ArrayCollection.
To do this, you can use onDelete="CASCADE"
/**
* @ORM\ManyToOne(targetEntity="Dish", inversedBy="typeAssoc", cascade={"persist"})
* @ORM\JoinColumn(name="dish_id",referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $dish;
Upvotes: 1