symfony2 实体表单类型中的选项组 [英] Option groups in symfony2 entity form type

查看:29
本文介绍了symfony2 实体表单类型中的选项组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在symfony2(v.2.1)的选项组中分组显示实体字段,例如我的表单类中有这样的东西:

$builder->add('account','实体',大批('类' =>'MyBundle\Entity\Account','query_builder' =>功能(实体库$repo){返回 $repo->findAllAccounts();},'必需' =>真的,'empty_value' =>'Choose_an_account',);

但是(当然)它们显示为存储库类从数据库中读取它,我想将它们分组显示在组合框中.这篇帖子提到开箱即用地添加到 2.2 版本中的功能,但我们 2.1 用户有哪些选择?>

分组将基于一个名为 Type 的字段,假设我在我的 Account 实体中有一个名为 getType() 的 getter,它返回一个字符串.

谢谢.

解决方案

我在处理类别时做了类似的事情.

首先,当您构建表单时,将选项列表作为函数 getAccountList() 的结果传递如下:

 public function buildForm(FormBuilderInterface $builder, array $options){$builder->add('账户', '实体', 数组('类' =>'MyBundle\Entity\Account','选择' =>$this->getAccountList(),'必需' =>真的,'empty_value' =>'Choose_an_account',));}

该函数应执行如下操作(内容取决于您构建结果的方式).

私有函数 getAccountList(){$repo = $this->em->getRepository('MyBundle\Entity\Account');$list = 数组();//现在你必须构造<optgroup>标签.假设有3组$list['group1'] = array();$list['group2'] = array();$list['group3'] = array();$accountsFrom1 = $repo->findFromGroup('group1');//检索您在 group1 中的帐户.foreach($accountsFrom1 作为 $account){$list[$name][$account->getName()] = $account;}//....等等返回 $list;}

当然,你可以做更多的动态!我的只是一个简单的例子!

您还必须将 EntityManager 传递给您的自定义表单类.所以,定义构造函数:

class MyAccountType 扩展 AbstractType {私人 $em;公共函数 __construct(\Doctrine\ORM\EntityManager $em){$this->em = $em;}}

并在您启动 MyAccountType 对象时传递 EntityManager.

Is there any way an entity field can be shown grouped in option groups in symfony2 (v.2.1), for example I have something like this in my form class:

$builder->add('account',
                    'entity',
                    array(
                        'class' => 'MyBundle\Entity\Account',
                        'query_builder' => function(EntityRepository $repo){
                            return $repo->findAllAccounts();
                        },
                        'required'  => true,
                        'empty_value' => 'Choose_an_account',
                    );

But (of course) they are displayed as the repository class reads it from the db and I would like to display them grouped in the combobox. This post mentions that featured added to the 2.2 release out of the box, but what options do we 2.1 users have?

The Grouping would be based on a field called Type, lets say I have a getter for that called getType() in my Account entity that returns a string.

Thanks.

解决方案

I did a similar thing when handling with categories.

First, when you build the form, pass the list of choices as a result from a function getAccountList() as follows:

 public function buildForm(FormBuilderInterface $builder, array $options){
        $builder        
            ->add('account', 'entity', array(
                'class' => 'MyBundle\Entity\Account',
                'choices' => $this->getAccountList(),
                'required'  => true,
                'empty_value' => 'Choose_an_account',
            ));
}  

The function should do something like follows (the content depend on the way you structure your result).

private function getAccountList(){
    $repo = $this->em->getRepository('MyBundle\Entity\Account');

    $list = array();

    //Now you have to construct the <optgroup> labels. Suppose to have 3 groups
    $list['group1'] = array();
    $list['group2'] = array();
    $list['group3'] = array(); 

    $accountsFrom1 = $repo->findFromGroup('group1'); // retrieve your accounts in group1.
    foreach($accountsFrom1 as $account){
        $list[$name][$account->getName()] = $account;
    }
    //....etc

    return $list;
} 

Of course, you can do it more dynamics! Mine is just a brief example!

You also have to pass the EntityManager to your custom form class. So, define the constructor:

class MyAccountType extends AbstractType {

    private $em;

    public function __construct(\Doctrine\ORM\EntityManager $em){
        $this->em = $em; 
    }    
} 

And pass the EntityManager when you initiate the MyAccountType object.

这篇关于symfony2 实体表单类型中的选项组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆