Symfony2实体字段显示选项为空时的所有条目 [英] Symfony2 entity field displaying all entries when choices are empty

查看:168
本文介绍了Symfony2实体字段显示选项为空时的所有条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Symfony2项目中有一个实体类型的表单字段。

  $ builder = $ this-> createFormBuilder(); 
$ projects = $ this-> getProjects();

$ builder-> add('project','entity',
array(
'class'=&'; MyBundle:Project',
' '=> false,
'choices'=> $ projects,
));

我遇到的问题是,当 getProjects() code>方法将返回一个空的结果集,下拉列表将包含所有项目表中的项目。



是有什么办法可以禁止这种行为?

解决方案

我相信意外的行为正在发生,因为您正在混淆使用Choice和实体表单字段类型。

您正在指定实体字段(第二个参数为$ builder-> add()),然后尝试使用选项选项填充值。但是,选择选项不直接适用于实体字段类型,尽管它被认为是从Choice继承的。相反,实体字段旨在为您自动从数据库加载选项。如果您只设置了实体类,则该字段将按照升序主键顺序与表中的所有实体一起填充。为了加载实体的子集和/或以特定的顺序加载它们,你可以设置一个'query_builder'函数。

例如,要创建一个下拉列表

  $ builder-> add('country',
'entity) ',
array('class'=>'My \Bundle\Entity\Country',
'property'=>'name',
'query_builder'=> ;函数(EntityRepository $ er){
return $ er-> createQueryBuilder('country')
- > orderBy('country.name','ASC');
},
'required'=> true,
'empty_value'=> false));

查询可以根据需要简单或复杂。请参阅使用Doctrine的查询生成器



我怀疑在问题中创建项目字段的方式会导致下拉列表的基础选项被设置两次 - 首先当设置了类选项时,到所有可用的项目实体,其次是'选择'选项设置,到$ this-> getProjects()的结果。假设后者是一个空数组,它不会覆盖前者,因此所有项目都会出现在列表中。



如果由于某种原因,您无法使用查询生成器来获取下拉项目,然后您可以使用选择字段类型并手动将项目数据映射到选项选项。例如,像这样:

  $ builder = $ this-> createFormBuilder(); 
$ projects = $ this-> getProjects();

$ projectChoices = array();
foreach($ projects作为$ project){
$ key = $ project-> getId();
$ value = $ project-> getName();
$ projectChoices [$ key] = $ value;


uilder-> add('project',
'choice',
array('choices'=> $ projectChoices,
'required'=> false));

请注意,在这种情况下,'project'字段的值将是Project id,而对于实体字段它将是一个实际的项目实体,这是为什么它最好使用实体字段的另一个原因。


I have an entity type form field in my Symfony2 project.

$builder = $this->createFormBuilder();
$projects = $this->getProjects();

$builder->add('project', 'entity',
        array(
            'class' => 'MyBundle:Project',
            'required' => false,
            'choices' => $projects,
        ));

The problem I'm having is, when the getProjects() method will return an empty result set, the drop down list will have all the projects in the Project table.

Is there any way to disable this behavior?

解决方案

I believe the unexpected behaviour is occurring because you are mixing up the use of the Choice and Entity form field types.

You are specifying an Entity field (second parameter to $builder->add()) and then attempting to populate it with values using the 'choices' option. However, the 'choices' option is not directly applicable to the Entity field type, although it is said to inherit from Choice. Rather, an Entity field is intended to automatically load the choices from the database for you. If you set only the Entity 'class', the field is populated with all the entities from the table in ascending primary key order. In order to load a subset of entities and/or load them in a particular order you can set a 'query_builder' function.

For example, to create a drop-down list of all countries in ascending name order:

$builder->add('country', 
              'entity', 
              array('class' => 'My\Bundle\Entity\Country',
                    'property' => 'name', 
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('country')
                                  ->orderBy('country.name', 'ASC');
                    },
                    'required' => true, 
                    'empty_value' => false));

The query can be as simple or complex as required. See Using Doctrine's Query Builder.

I suspect that the way the project field is created in the question causes the underlying choices for the drop-down to be set twice - firstly when the 'class' option is set, to all the available project entities, secondly when the 'choices' option is set, to the result of $this->getProjects(). Presumably if the latter is an empty array it does not override the former and thus all the projects appear in the list.

If for some reason you can't use a query builder to get the projects for the drop-down then you can use a Choice field type and map the projects data into the 'choices' option manually. For example, something like this:

$builder = $this->createFormBuilder();
$projects = $this->getProjects();

$projectChoices = array();
foreach ($projects as $project) {
    $key = $project->getId();
    $value = $project->getName();
    $projectChoices[$key] = $value;
}

$builder->add('project', 
              'choice',
               array('choices' => $projectChoices,
                     'required' => false));

Note that in this case the value of the 'project' field will be a Project id whereas for an Entity field it will be an actual Project entity, which is another reason why it is preferable to use an Entity field.

这篇关于Symfony2实体字段显示选项为空时的所有条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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