php - Symfony2 duplicates collection entities when form index is missing -
i using symfony 2 , form component manage relations.
$builder->add('locations', 'collection', array( 'type' => new locationtype(), 'options' => array("em" => $this->em), "allow_add" => true, "by_reference" => false, "allow_delete" => true, "delete_empty" => true ));
i use many 2 many relation fields, use many-to-one , one-to-many
it works want. new entities can created. delete entities, provide ajax function removes item db , deletes corresponding html part of form.
when have 2 entities in collection, delete first 1 , not reload form, form has 1 entity element left. 1 has index 1 , not 0. when submit form, have last entity duplicated. not want!
the problem occurs when delete first location, when there "index gap".
how can avoid this? there option?
found solution.
in thr pre_bind event this:
$data["locations"] = array_values($data["locations"]);
this removes gaps array
Comments
Post a Comment