duanliao5995 2015-07-23 08:47
浏览 51

Symfony2在表单中创建新的实体元素

I am using SonataAdminBundle and in the form I want to display some mapped information. My main object Skin is mapped with CmsElemnt, in witch the content is saved. I need to acces it in the form to be able to update it.

I have this 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 the symfony dcumentation Here:

http://symfony.com/doc/current/book/forms.html#embedding-a-single-object

So I created a service to load the 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 the skin admin I do 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 get this error:

Please define a type for field `skin_element_type_cms` in `Mp\ShopBundle\Admin\SkinAdmin`
  • 写回答

1条回答 默认 最新

  • duanli0119 2015-07-23 13:59
    关注

    You don't have to create a custom form to manage your relations. You can use the sonata_type_collection to 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 the official documentation for more details : https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-one-to-many

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题