Delete an entity without to delete the foreign key link to it

Viewed 1691

I have a table Products the entity tva is link to the table Tva and the entity image is link to the table Media. I render in sonata admin all the products of the website. I would like to be able to delete a product from the administration.

I want be able to delete a product without to delete the foreign key link to it.

But i have this error:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (fly.post, CONSTRAINT FK_5A8A6C8D4D79775F FOREIGN KEY (tva_id) REFERENCES tva (id))

This is my entity tva and image

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=true)
 */

private $tva;

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Media", cascade={"persist","remove"})
 * @ORM\JoinColumn(nullable=true)
 */
private $image;

I decided to change the join column from this * @ORM\JoinColumn(nullable=true) to this * @ORM\JoinColumn(onDelete="CASCADE") and it works i can delete the product but it also delete the foreign key and i don't want that because i have other product that use the same image and tva.

2 Answers
Related