Symfony2 Form Builder - 从数据库查询中创建一系列选择 [英] Symfony2 Form Builder - creating an array of choices from a DB query

查看:18
本文介绍了Symfony2 Form Builder - 从数据库查询中创建一系列选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 FormType 类中,我在 buildForm 方法中有这个:

In my FormType class I have this in the buildForm method:

//...
->add('businessUnit', 'entity', array(
                'class' => 'TrainingBundle:Employee',
                'attr' => array('class' => 'form-control select2'),
                'property' => 'businessUnit',
                'empty_value' => 'All Business Units',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->groupBy('e.businessUnit')
                        ->orderBy('e.businessUnit', 'ASC')
                        ;
                },
                'required' => false
//...

这很好用,除了将businessUnit"放在 标签的值中之外,我得到了员工 ID.我需要的是 Employee 类中包含所有不同业务单元的下拉列表.也许我应该使用 choice 而不是 entity ,但是我不确定如何生成选择数组.

This works fine, except instead of the "businessUnit" being put in the value of the <option> tags I get the employee ID. What I need is a dropdown with all distinct businessUnits in the Employee class. Perhaps I should be using choice instead of entity , but then I am not sure how to generate the array of choices.

答案如已接受的答案中所述,我使用此功能

ANSWER As described in the accepted answer I make this function

 private function fillBusinessUnit() {
        $er = $this->em->getRepository('TrainingBundle:Employee');

        $results = $er->createQueryBuilder('e')
               ->groupBy('e.businessUnit')
               ->orderBy('e.businessUnit', 'ASC')
               ->getQuery()
               ->getResult()
               ;

        $businessUnit = array();
        foreach($results as $bu){
             $businessUnit[$bu->getBusinessUnit()] = $bu->getBusinessUnit();
        }

        return $businessUnit;
    }

必须将 EntityManager 传递给表单.而且还放在表单顶部使用Doctrine\ORM\EntityManager;

Had to pass in the EntityManager to the form. And also put use Doctrine\ORM\EntityManager; at top of the form

推荐答案

改用 choice.它必须用数组设置,因此创建一个方法来完成它.

Use choice instead. It has to be set with an array, so create a method to do it.

->add("type", "choice",
      array("label" => "Type",
            "choices" => $this->fillBusinessUnit(),
            "attr" => array("class" => "form-control select2"), 
            "empty_value" => 'All Business Units'))

在此方法中,您只需使用 QueryBuilder 运行查询,然后循环结果,填充数组并返回.

In this method you just have to run your query with the QueryBuilder, then loop the results, fill an array and return it.

private function fillBusinessUnit() {

    $results = $er->createQueryBuilder('e')
               ->groupBy('e.businessUnit')
               ->orderBy('e.businessUnit', 'ASC');

    $businessUnit = array();
    foreach($results as $bu){
         $businessUnit[] = array("id" => $bu->getId(), "name" => $bu->getName()); // and so on..
    }

    return $businessUnit;
}

编辑

我猜你在 Controller 中实例化了你的类型,这样你就可以在类型构造中传递它:

I guess you instantiate your Type in a Controller so you can pass it in the Type construct:

$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new YourType($em));

然后在你的表单类 YourType.php 中:

then in your form class YourType.php:

class YourType extends AbstractType {

    private $em;

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

希望这有帮助:)

这篇关于Symfony2 Form Builder - 从数据库查询中创建一系列选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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