php - Symfony2 create new entity element in the form itself -
i using sonataadminbundle , in form want display mapped information. main object skin mapped cmselemnt, in witch content saved. need acces in form able update it.
i have mapped information:
skin.php
/** * @orm\onetomany(targetentity="cmselement", mappedby="content") */ private $navbar;
cmselement.php
/** * @orm\manytoone(targetentity="skin", inversedby="navbar") * @orm\joincolumn(name="page_id", referencedcolumnname="id") */ private $content;
i tried using symfony dcumentation here:
http://symfony.com/doc/current/book/forms.html#embedding-a-single-object
so created service load cmselement information:
class skinelementtype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('content', 'textarea'); } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'mp\shopbundle\entity\cmselement', )); } public function getname() { return 'skin_element_type_cms'; } }
registered it:
skin_element_type_cms: class: mp\shopbundle\form\type\skinelementtype tags: - { name: form.type }
now in skin admin this:
/** * @param formmapper $formmapper */ protected function configureformfields(formmapper $formmapper) { $formmapper // ->add('id', 'hidden') ->add('name') ->add('status', null, array('required' => false)) ->add('skin_element_type_cms', new cmselement(), array( 'required' => false, 'attr' => array('cols' => '8', 'rows' => '8'))) }
i error:
please define type field `skin_element_type_cms` in `mp\shopbundle\admin\skinadmin`
you don't have create custom form manage relations. can use sonata_type_collection manage onetomany relations.
skinadmin.php
protected function configureformfields(formmapper $formmapper) { $formmapper ->add('name') ->add('status', null, array('required' => false)) ->add('navbar', 'sonata_type_collection', array(), array( 'edit' => 'inline', 'inline' => 'table' )) ; }
check official documentation more details : https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-one-to-many
Comments
Post a Comment