Symfony2:表单抛出“传递给Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()的参数1必须是一个数组,对象在提交时”

时间:2014-01-20 13:08:29

标签: forms symfony fosuserbundle

我在我的一个项目中使用FOSUserBundle。

我基于Employee对象构建了一个表单(与RoleGroup有很多共同点)。

这是表格(部分内容):

$builder->add('groups', 'entity', array(
    'class' => 'MMAAuthBundle:RoleGroup',
    'choices' => $this->groups,
    'property' => 'name',
    'label' => 'Groups',
    'expanded' => true,
    'attr' => array("multiple" => true)
));

当我提交表单时,我在Profiler中收到此错误:

    at ErrorHandler ->handle ('4096', 'Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given, called in /home/mihai/intranet/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 528 and defined', '/home/mihai/intranet/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php', '47', array()) 
in /home/mihai/intranet/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php at line 47

如何让表单返回ArrayCollection,而不是RoleGroup对象?

之前我遇到了this问题,但现在我被困在这里了。

1 个答案:

答案 0 :(得分:3)

您的表单目前是一个多表单,因此将RoleGroup个对象而不是RoleGroup个对象数组传递给Collection的构造函数

multiple表单选项 ...而不是HTML属性。因此...

$builder->add('groups', 'entity', array(
// This would only render a multiple="true" inside the fields HTML tag
'attr' => array("multiple" => true)

......应该......

$builder->add('groups', 'entity', array(
// multiple option not wrapped by attribute is correct
"multiple" => true