I have a create form for an entity which works the way it is supposed to. The entity for this form is related ManyToMany with another entity, and that other entity is used to populate a multiple select field.
When I save the create form and select multiple options from that multiple select field, the associations between the form type's entity and the related entity are saved correctly.
However, I also have an edit form which uses the same form type class and template. When I load the edit form and the information for the existing entity is filled into it automatically, I see that the relationships with the multiple select field entity are indeed reflected; the appropriate fields of the multiple select box are selected.
When I go to save the edit form is when the problem arises. If I DEselect options in the multiple select field, I would expect that upon saving the edit form that those options would become disassociated, but this is not the case. Instead, the original relationships are preserved as if no options were changed.
What else do I need to do so that when I DEselect options on the multiple select, those options will become disassociated? Thank you!
In the Entity:
/**
* unidirectional ManyToMany
* @ORM\ManyToMany(targetEntity="\Myco\ClientBundle\Entity\Country", cascade={"persist"}, orphanRemoval=true)
* @ORM\JoinTable(name="offer_country",
* joinColumns={@ORM\JoinColumn(name="offer_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")}
* )
* */
private $countries;
...
/**
* This is method getCountries
*
* @return ArrayCollection
*
*/
public function getCountries()
{
return $this->countries;
}
In the form type, here's how the multiple select box is created:
->add('countries', 'entity', [
'class' => 'ClientBundle:Country',
'property' => 'name',
'required' => false,
'expanded' => false,
'multiple' => true,
'mapped' => true
])
In the controller, here are the functions having to do with editing:
/**
* Displays edit form
*
* @throws NotFoundHttpException
*
* @param Request $request
* @param int $id
*/
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offer = $em->getRepository('ClientBundle:Offer')->find($id);
if (!$offer)
{
throw new NotFoundHttpException('Offer not found!');
}
$this->setEditForm($offer, '/offer/editPost/' . $id);
return [
'form' => $this->form->createView(),
'devicesForm' => $this->getDevicesForm('/devices/listJson')->createView(),
'gate' => $this->gate
];
}
/**
* Handles submission of edit form
*/
public function editPostAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offer = $em->getRepository('ClientBundle:Offer')->find($id);
if (!$offer)
{
throw new NotFoundHttpException('Offer not found!');
}
$this->setEditForm($offer);
$this->form->handleRequest($request);
if (!$this->form->isValid())
{
throw new InvalidArgumentException('Unable to save this item due to invalid argument(s).');
}
return ['form' => $this->form->createView()];
}
/**
* Sets up $this->form as a new edit form with the specified Offer
*
* @param Offer $offer
*/
protected function setEditForm($offer, $action = '')
{
$this->setOffer($offer);
$this->form = $this->createForm(new OfferType, $this->offer, ['action' => $action]);
$this->form->add('submit', 'submit');
}
/**
* Sets $this->offer
* @param Offer $offer
*/
protected function setOffer(Offer $offer = null)
{
if (!$offer)
{
$offer = new Offer;
}
$this->offer = $offer;
}