php - Embedding a single object in form -


i've manage create form embedded in form think i'm not doing right. here's code

category

   class category     {         private $id;          private $name;          /**          * @orm\onetomany(targetentity="category", mappedby="category")          */         private $subcategorues;          public function __construct()         {             $this->subcategorues = new \doctrine\common\collections\arraycollection();         }           public function getid()         {             return $this->id;         }          public function setname($name)         {             $this->name = $name;              return $this;         }          public function getname()         {             return $this->name;         }           public function addsubcategorue(\appbundle\entity\category $subcategorues)         {             $this->subcategorues[] = $subcategorues;              return $this;         }          public function removesubcategorue(\appbundle\entity\category $subcategorues)         {             $this->subcategorues->removeelement($subcategorues);         }          public function getsubcategorues()         {             return $this->subcategorues;         }      } 

subcategory

class subcategory {     private $id;      private $name;      /**      * @orm\manytoone(targetentity="category", inversedby="subcategories")      * @orm\joincolumn(name="category_id", referencedcolumnname="id")      */     private $category;      /**      * @return mixed      */     public function getcategory()     {         return $this->category;     }      /**      * @param mixed $category      */     public function setcategory($category)     {         $this->category = $category;     }     /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }       public function setname($name)     {         $this->name = $name;          return $this;     }      public function getname()     {         return $this->name;     } } 

categorytype

.......  public function buildform(formbuilderinterface $builder, array $options)     {         $builder             ->add('name', 'entity', [                 'class' => 'appbundle\entity\category',                 'choice_label' => 'name'             ]);     }   public function configureoptions(optionsresolver $resolver) {     $resolver->setdefaults([         'data_class' => 'appbundle\entity\category'     ]); } ...... 

subcategorytype

 $builder             ->add('category', new categorytype(), [                 'label' => false             ])             ->add('name', 'text')             ->add('save', 'submit')             ;         public function configureoptions(optionsresolver $resolver) {     $resolver->setdefaults([         'data_class' => 'appbundle\entity\subcategory'     ]); } 

defaultcontroller

public function indexaction(request $request)     {          $subcategory = new subcategory();          $form = $this->createform(new subcategorytype(), $subcategory);          $form->handlerequest($request);          if($form->isvalid()){              $em = $this->getdoctrine()->getmanager();             $subcategory->setcategory($subcategory->getcategory()->getname());              $em->persist($subcategory);             $em->flush();              return new response(sprintf('id %d', $subcategory->getid()));         }          return $this->render('appbundle::layout.html.twig', [           'form' => $form->createview(),         ]);     } 

please notice line of code $subcategory->setcategory($subcategory->getcategory()->getname()); need line in order save entity database otherwise error. question is there way skip line of code , pass category object on fly subcategory->category property instead of doing manually?

//edit

here's output of dump($form->getdata());

defaultcontroller.php on line 33: subcategory {#467 ▼   -id: null   -name: "uncharted"   -category: category {#588 ▼     -id: null     -name: category {#685 ▼       -id: 2       -name: "games"       -subcategorues: persistentcollection {#686 ▶}     }     -subcategorues: arraycollection {#660 ▶}   } } 

your categorytype not correctly mapped compared category entity. actually, in case, don't need have sub-form categorytype name field, since have category field in subcategory relationship towards category.

just replace:

->add('category', new categorytype(), [             'label' => false         ]) 

by:

->add('category', 'entity', [             'class' => 'appbundle\entity\category',             'choice_label' => 'name'         ]); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -