Enregistrements non supprimés

Par dubitoph, il y a 7 ans


Bonjour,

J'ai un souci pour supprimer des enregistrements en base de données.

J'ai 4 entités : Advert, Insurance, InsurancePrice et Duration. Une assurance est liée à une annonce et référence différents prix. Chaque prix est lié à une durée.

Pour réaliser ceci, voici le code probant dans chacune des entités :

Dans Advert.php :

/** * @ORM\OneToOne(targetEntity="App\Entity\Insurance", mappedBy="advert", cascade={"persist", "remove"}) */ private $insurance;

Dans Insurance.php :

... /** * @ORM\OneToMany(targetEntity="App\Entity\InsurancePrice", mappedBy="insurance", cascade={"persist", "remove"}) */ private $insurancePrices; public function __construct() { $this->insurancePrices = new ArrayCollection(); } ... /** * @return Collection|InsurancePrice[] */ public function getInsurancePrices(): Collection { return $this->insurancePrices; } public function addInsurancePrice(InsurancePrice $insurancePrice): self { if (!$this->insurancePrices->contains($insurancePrice)) { $this->insurancePrices[] = $insurancePrice; $insurancePrice->setInsurance($this); } return $this; } public function removeInsurancePrice(InsurancePrice $insurancePrice): self { if ($this->insurancePrices->contains($insurancePrice)) { $this->insurancePrices->removeElement($insurancePrice); // set the owning side to null (unless already changed) if ($insurancePrice->getInsurance() === $this) { $insurancePrice->setInsurance(null); } } return $this; } ...

Dans insurancePrice.php :

/** * @ORM\ManyToOne(targetEntity="App\Entity\Duration") * @ORM\JoinColumn(nullable=false) */ private $duration; /** * @ORM\ManyToOne(targetEntity="App\Entity\Insurance", inversedBy="insurancePrices") * @ORM\JoinColumn(nullable=false) */ private $insurance;

Dans mon controller, après avoir supprimé les prix, je tente d'enregistrer mon annonce :

... if ($formCosts->isSubmitted() && $formCosts->isValid()) { $insurance->setIncluded($formCosts['insurance']['included']->getData()); $insurance->setDeductible($formCosts['insurance']['deductible']->getData()); $i = 0; $insuranceIncluded = $insurance->getIncluded(); foreach ($insurancePrices as $insurancePrice) { $price = $formCosts['insurancePrices'][$i]['price']->getData(); if ($insuranceIncluded) { $insurance->removeInsurancePrice($insurancePrice); } else { $insurancePrice->setPrice($price); } $i++; } ... $manager->persist($advert); $manager->flush();

Cependant, au moment du persist(), j'obtiens cette erreur :

"
An exception occurred while executing 'UPDATE insurance_price SET insurance_id = ? WHERE id = ?' with params [null, 1]:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (roadtrip.insurance_price, CONSTRAINT FK_651C18DAD1E63CD1 FOREIGN KEY (insurance_id) REFERENCES insurance (id))
"

Je ne comprends pas d'où provient l'erreur. Quelqu'un aurait une idée?

Merci d'avance pour votre aide.

1 réponse

dubitoph, il y a 7 ans

J'ai modifié l'annotation de l'attribut $insurancePrices dans l'entité Insurance, et tout est maintenant ok :

/** * @ORM\OneToMany(targetEntity="App\Entity\InsurancePrice", mappedBy="insurance", cascade={"persist"}, orphanRemoval=true) */ private $insurancePrices;