symfony - Symfony2 with entity type in form -


here situation, have form in need entity field type. inside benefitgroup entity have benefitgroupcategory selection.

my buildform is:

public function buildform(formbuilderinterface $builder, array $options) {     $builder->add('benefitgroupcategories', 'entity', array(                 'class' => 'appbundle:benefitgroupcategory',                 'property' => 'name',                 'label' => false,                 'query_builder' => function(entityrepository $er) {                     return $er->createquerybuilder('c')                     ->orderby('c.name', 'asc');                 },))             ->add('benefitsubitems', 'collection', array('type' => new benefitsubitemformtype(), 'allow_add'    => true, 'label' => false,));  } 

it's typical product-category relationship. benefitgroup can have 1 category , category can belong many benefitgroups (the complication, not implemented yet, that's reason need query builder, depend on parameter (project) categories default ones (always available), others available specific projects (see below reference project in benefitgroupcategory entity)).

you'll notice field, benefitsubitems, not relevant question @ hand.

as far understand it, doctrine perspective, have set one-to-many, unidirectional join table.

the 2 entities are:

<?php // src/appbundle/entity/benefitgroup.php  namespace appbundle\entity; use doctrine\common\collections\arraycollection;  use doctrine\orm\mapping orm;  /**  * @orm\entity(repositoryclass="appbundle\entity\benefitgrouprepository")  * @orm\table(name="benefit_groups")  */ class benefitgroup { /**  * @orm\column(type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @orm\manytoone(targetentity="benefititem", cascade={"persist"}, inversedby="benefitgroups")  */ protected $benefititem;  /**  * @orm\onetomany(targetentity="benefitsubitem", mappedby="benefitgroup")  */     protected $benefitsubitems;   /**  * @orm\manytomany(targetentity="benefitgroupcategory")  * @orm\jointable(name="benefitgroup_benefitgroupcategory", joincolumns={@orm\joincolumn(name="benefitgroup_id", referencedcolumnname="id")}, inversejoincolumns={@orm\joincolumn(name="benefitgroupcategory_id", referencedcolumnname="id", unique=true)})  */     protected $benefitgroupcategories;  // here have irrelevant getters , setters      /**  * constructor  */ public function __construct() {     $this->benefitsubitems = new arraycollection();     $this->benefitgroupcategories = new arraycollection(); }  /**  * add benefitgroupcategories  *  * @param \appbundle\entity\benefitgroupcategory $benefitgroupcategories  * @return benefitgroup  */ public function addbenefitgroupcategory(\appbundle\entity\benefitgroupcategory $benefitgroupcategories) {     $this->benefitgroupcategories[] = $benefitgroupcategories;      return $this; }  /**  * remove benefitgroupcategories  *  * @param \appbundle\entity\benefitgroupcategory $benefitgroupcategories  */ public function removebenefitgroupcategory(\appbundle\entity\benefitgroupcategory $benefitgroupcategories) {     $this->benefitgroupcategories->removeelement($benefitgroupcategories); }  /**  * benefitgroupcategories  *  * @return \doctrine\common\collections\collection   */ public function getbenefitgroupcategories() {     return $this->benefitgroupcategories; } } 

you'll notice entity, benefititem, "father" of benefitgroup.

and

<?php // src/appbundle/entity/benefitgroupcategory.php  namespace appbundle\entity;  use doctrine\orm\mapping orm; use symfony\bridge\doctrine\validator\constraints\uniqueentity;  /**  * @orm\entity()  * @orm\table(name="benefit_group_category")  * @uniqueentity(fields={"name", "project"}, ignorenull=false, message="duplicated group category project")  */ class benefitgroupcategory { /**  * @orm\column(type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @orm\column(type="string", length=50)  */ protected $name; /**  * @orm\manytoone(targetentity="project")  */ protected $project;  // here have irrelevant getters , setters      } 

in controller (you'll see several embedded collections, work ok) have:

/**  * @route("/benefit/show/{projectid}", name="benefit_show")  */ public function showaction(request $request, $projectid) {     $id=4; //the id of cvc     $storedcvc = $this->getdoctrine()                       ->getrepository('appbundle:cvc')                       ->find($id);     $form = $this->createform(new cvcformtype(), clone $storedcvc);      $form->handlerequest($request);      if ($form->isvalid())         {         $em = $this->getdoctrine()->getmanager();         //$benefitgroupcategoryrepository = $this->getdoctrine()->getrepository('appbundle:benefitgroupcategory');         $formcvc = $form->getdata();         $em->persist($formcvc);         foreach ($formcvc->getbenefititems() $formbi)             {             $newbi = new benefititem();             $newbi->setcvc($formcvc);             $newbi->setcomment($formbi->getcomment());             $em->persist($newbi);              foreach ($formbi->getbenefitgroups() $formbg)                 {                 $newbg = new benefitgroup();                 $newbg->setbenefititem($newbi);                 $newbg->setbenefitgroupcategories($formbg->getbenefitgroupcategories());                 $em->persist($newbg);                  foreach ($formbg->getbenefitsubitems() $formsi)                     {                     $newsi = new benefitsubitem();                     $newsi->setbenefitgroup($newbg);                     $newsi->setcomment($formsi->getcomment());                     $em->persist($newsi);                     }                 }             }             $em->flush();                     }      return $this->render('benefit/show.html.twig', array(         'form' => $form->createview(),     )); } 

the problem is: in visualization visualizes correctly form (even though not retrieve correctly category. have choice of categories, ok, not retrieve right one. do have set default value in form?

the problem gets way worse when sumbit form it's supposed create new entity (notice clone) nested ones. problem crashes saying:

neither property "benefitgroupcategories" nor 1 of methods "addbenefitgroupcategory()"/"removebenefitgroupcategory()",  "setbenefitgroupcategories()", "benefitgroupcategories()", "__set()" or  "__call()" exist , have public access in class  "appbundle\entity\benefitgroup". 

the "beauty" even if comment completeley (nasty) part inside "isvalid" behaves same.

i'm lost :(

about cloning have unset id of cloned entity, here: https://stackoverflow.com/a/14158815/4723525

edit:

yes, php shallow copy, have clone other objects. @ example #1 cloning object in http://php.net/manual/en/language.oop5.cloning.php. have clone objects defining __clone method (for doctrine lower 2.0.2 have calling own method after cloning because proxy defines it's own __clone method). example:

function __clone() {    $oldcollection = $this->collection;    $this->collection = new arraycollection();    foreach ($oldcollection $oldelement) {        $newelement = clone $oldelement;        // additional actions example setting object owning side        $newelement->setparent($this);        $this->collection->add($newelement);    } } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -