In a Doctrine entity I've got this configuration:
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\ManyToMany(targetEntity="PriceRate", cascade={"all"}, orphanRemoval=true)
* @ORM\JoinTable(name="product_rates",
* joinColumns={@ORM\JoinColumn(name="product_id",referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="rate_id",referencedColumnName="id")})
*/
protected $rates;
When I delete the entity it tries to delete the price_rate table first, instead of the joined table so I get the following error:
Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
uniski.product_rates, CONSTRAINTFK_15A90A8FBC999F9FFOREIGN KEY (rate_id) REFERENCESprice_rate(id))
Why is it not trying to delete the joined table rows first? I've tried to add onDelete statements on the join table columns but it didn't work.
This is a unidirectional relationship because PriceRate is used by other entities, therefore i'm using a ManyToMany relationship.
The only way it works is before removing the entity is to clear the ArrayCollection of the child entities like so:
$product->removeAllRate(); //it does this: $this->rates->clear();
$em->remove($product);
$em->flush();
Thanks!