'Symfony - EasyAdmin - Add and remove functions are ignored from AssociationField
I use the last version of EasyAdmin and my add
and remove
functions are ignored when I submit the form:
Ambiance
entity:
/**
* @ORM\OneToMany(targetEntity="Vehicule", mappedBy="ambiance")
*/
protected Collection $vehicules;
public function __construct()
{
$this->vehicules = new ArrayCollection();
}
public function addVehicule(Vehicule $vehicule): self
{
if (!$this->vehicules->contains($vehicule)) {
$this->vehicules[] = $vehicule;
$vehicule->setAmbiance($this);
}
return $this;
}
public function removeVehicule(Vehicule $vehicule): void
{
if (!$this->vehicules->contains($vehicule)) {
return;
}
$this->vehicules->removeElement($vehicule);
}
public function getVehicules()
{
return $this->vehicules;
}
public function setVehicules($vehicules): void
{
$this->vehicules = $vehicules;
}
Yet my Doctrine mapping is valid..
My EasyAdmin form in AmbianceCrudController.php
:
'vehicules' => AssociationField::new('vehicules', 'Véhicules'),
It generates a multiple select2
but when I add vehicles and submit my form, no data is inserted.
Solution 1:[1]
Replace
AssociationField::new('vehicules')
by
AssociationField::new('vehicules', 'VĂ©hicules')->setFormTypeOption('by_reference', false)
Solution 2:[2]
I'm strugling with the same problem, but I found already bits of the puzzle that could be helpful:
- in my experience, it seems that the owning side of the association (the one that has the foreign key in the database) results in an edit form that persists the change.
- the other side ignores the change completely
- the setters and getters, and addABC and removeABC methods seem indeed to be skipped for reasons that are unclear to me as well.
I hope you can find a solution with the hint that the crudcontroller other side in the relationship, I think Vehicules in your case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Paul ALBERT |
Solution 2 | pieterdt |