php - EntityType Dual list Symfony Form -


i using symfony3 framework , struggling 1 of forms... have 2 entities;

  1. object
  2. element

i trying implement dual list (multiselect) box. have succeeded untill image below: (so got front-end going on, it's something... :s) enter image description here

with code below can choose select (or de-select) "element" entities. form submitted , persisted (succesfully!).

but when select 1 "element" can submit form error:

an exception occurred while executing 'insert objects_elements (object_id, element_id) values (?, ?)' params [3, 1]:

when deselect 1 "element" no error element stays in selected box.

this code:

// ... class object {  /**  * @orm\id  * @orm\column(type="integer")  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @orm\manytomany(targetentity="element", inversedby="objects")  * @orm\jointable(name="objects_elements")  */ protected $elements;  // ...  } 

class element {  /**  * @orm\id  * @orm\column(type="integer")  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  *  * @orm\column(type="string", nullable=true)  */ protected $name;  /**  * @orm\manytomany(targetentity="object", mappedby="elements")  */ private $objects; 

my formtype looks this

class objecttype extends abstracttype {  /**  * @param formbuilderinterface $builder  * @param array $options  */ public function buildform(formbuilderinterface $builder, array $options) {      $builder             ->add('elements', entitytype::class, array(                 'class' => 'appbundle:element',                 'multiple' => true,                 'empty_data'  => null,                 'required'    => false,             ))     ; } 

my controller (i post editaction) looks this:

/**  * displays form edit existing object entity.  *  * @route("/{id}/edit", name="object_edit")  * @method({"get", "post"})  */ public function editaction(request $request, object $object) {     $deleteform = $this->createdeleteform($object);     $editform = $this->createform('appbundle\form\objecttype', $object);     $editform->handlerequest($request);      if ($editform->issubmitted() && $editform->isvalid()) {         $em = $this->getdoctrine()->getmanager();         $em->persist($object);         $em->flush();          return $this->redirecttoroute('object_edit', array('id' => $object->getid()));     }      return $this->render('object/edit.html.twig', array(                 'object' => $object,                 'edit_form' => $editform->createview(),                 'delete_form' => $deleteform->createview(),     )); } 


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -