Symfony 2表单创建用户并添加组 [英] Symfony 2 Form Create user and add Group
问题描述
$ builder-> add('firstname','文本',数组(
'必需'=&';'required'
))
- > add('middlename')
- > add('lastname')
- > add('email','email')
- > add('isActive');
但我想添加一个组。
我有一个实体Group和一个表单GroupType。
但是,如何添加一个选择与我所有的组选择一个或多个?
我试过了:
- > add('groups','choice')
但出现此错误:
注意:Doctrine\Common\Collections\ArrayCollection类的对象不能在/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php中将其转换为int 457行
如何解决此问题?
创建一个填充所有组来自数据库的对象,使用'实体'字段。要启用多项选择(1项以上选项),请使用多项选项。例如:
$ b $ pre $ code $ builder-> add('groups','entity',array(
'class '=>'YourBundle:Group',
'property'=>'name',
'multiple'=> true,
));
'property'选项的值决定显示哪个Group字段。如果您需要订购选择元素中的对象或显示对象的子集,请使用'query_builder'选项使用自定义查询加载选择。例如:
使用Doctrine \ORM \EntityRepository;
// ...
$ builder-> add('groups','entity',array(
'class'=>'YourBundle:Group',
'property'=>'name',
'multiple'=> true,
'query_builder'=>函数(EntityRepository $ er){
return $ er - > createQueryBuilder('g')
- > orderBy('g.name','ASC');
},
));
请参阅实体字段类型在Symfony2文档中。
I want to create a form to add a user:
$builder->add('firstname', 'text', array(
'required' => 'required'
))
->add('middlename')
->add('lastname')
->add('email', 'email')
->add('isActive');
but I want to add also one group. I have a entity "Group" and a form "GroupType". But how to add a choice with all my groups to select one or multiple?
I tried:
->add('groups', 'choice' )
but getting this error:
Notice: Object of class Doctrine\Common\Collections\ArrayCollection could not be converted to int in /vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457
How to fix this?
To create a choice field populated with all the Group objects from the database, use an 'entity' field. To enable multiple selection (1+ choices) use the 'multiple' option. For example:
$builder->add('groups', 'entity', array(
'class' => 'YourBundle:Group',
'property' => 'name',
'multiple' => true,
));
The value of the 'property' option determines which Group field is displayed. If you need to order the objects in the choice element or display a subset of the objects, use the 'query_builder' option to load the choice using a custom query. For example:
use Doctrine\ORM\EntityRepository;
// ...
$builder->add('groups', 'entity', array(
'class' => 'YourBundle:Group',
'property' => 'name',
'multiple' => true,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')
->orderBy('g.name', 'ASC');
},
));
See entity Field Type in the Symfony2 documentation.
这篇关于Symfony 2表单创建用户并添加组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!