Error : the new value must be an array or an instance of \Traversable, Entity class given

Error : the new value must be an array or an instance of \Traversable, Entity class given

我在 PostCategory 之间有一个 manyToMany 关联。我正在尝试提交 Post 表单以创建一个 post,其中包含从下拉列表 <select>.

中选择的类别

表单正在正确呈现使用:

<div id="form-categories-widget">
    <select id="shop_bundle_managementbundle_posttype_categories" 
        name="shop_bundle_managementbundle_posttype[categories]">
        {% for key,val in form.categories.vars.choices %}
        <option value="{{ val.value }}" {{  form.categories.vars.value == '' and key == 0 ? ' selected ' :(val.value == form.categories.vars.value ? ' selected ' : '') }}
            >
            {{ val.data.getName | trans }} 
        </option>
        {% endfor %}
    </select>
</div> 

问题:

当我点击提交按钮时出现以下错误(为此我花了大约 2 天时间试图弄清楚):

The property "categories" in class "Shop\Bundle\ManagementBundle\Entity\Post" can be defined with the methods "addCategory()", "removeCategory()" but the new value must be an array or an instance of \Traversable, "Shop\Bundle\ManagementBundle\Entity\Category" given.

这是我的表单类型和实体(如果它们有用的话)。提前感谢您抽出宝贵的时间和一如既往的帮助。

实体Post:

<?php

namespace Shop\Bundle\ManagementBundle\Entity;

class Post
{
.....
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $categories;


    /**
     * Add category
     *
     * @param \Shop\Bundle\ManagementBundle\Entity\Category $category
     *
     * @return Post
     */
    public function addCategory(\Shop\Bundle\ManagementBundle\Entity\Category $category)
    {
        $this->categories[$category->getName()] = $category;
        $category->addPost($this);
        return $this;
    }

    /**
     * Remove category
     *
     * @param \Shop\Bundle\ManagementBundle\Entity\Category $category
     */
    public function removeCategory(\Shop\Bundle\ManagementBundle\Entity\Category $category)
    {
        $this->categories->removeElement($category);
    }

    /**
     * Get categories
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCategories()
    {
        return $this->categories;
    }


}

Post类型

class PostType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('categories', 'entity', array(
                'multiple' => false,   // Multiple selection allowed
                'expanded' => false,   // Render as checkboxes
                'class' => 'ShopManagementBundle:Category',
                'property'     => 'name'
            ));

    }
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Shop\Bundle\ManagementBundle\Entity\Post'

        ));
    }
    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'shop_bundle_managementbundle_posttype';
    }

}

您的字段 'categories' 需要是实体的 CollectionType 而不仅仅是实体。

在您的示例中,您尝试用类别实体替换作为集合的 $categories。

读这个:CollectionType Field

但我认为存在一个误区:你试图将多个类别赋予一个post? 你把事情弄反了吗?一个类别包含多个帖子 ?

http://symfony.com/doc/current/reference/forms/types/collection.html

问题出在表单模板上,因为我手动编码时忘记了双括号 []。所以代替:

<select id="shop_bundle_managementbundle_posttype_categories" 
        name="shop_bundle_managementbundle_posttype[categories]">

我应该一直在使用:

<select id="shop_bundle_managementbundle_posttype_categories" 
        name="shop_bundle_managementbundle_posttype[categories][]">

希望其他人不要犯同样的错误

谢谢