【问题标题】:Error : the new value must be an array or an instance of \Traversable, Entity class given错误:新值必须是数组或 \Traversable 的实例,给定的实体类
【发布时间】:2017-01-09 06:34:53
【问题描述】:

我在PostCategory 之间有一个manyToMany 关联。我正在尝试提交帖子表单以从下拉列表<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 天的时间试图弄清楚):

类中的属性“类别” "Shop\Bundle\ManagementBundle\Entity\Post" 可以用 方法“addCategory()”、“removeCategory()”,但新值必须是 \Traversable 的数组或实例, “Shop\Bundle\ManagementBundle\Entity\Category”给出。

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

实体帖子:

<?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;
    }


}

PostType

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';
    }

}

【问题讨论】:

    标签: php forms symfony doctrine-orm many-to-many


    【解决方案1】:

    您的“类别”字段必须是实体的 CollectionType,而不仅仅是实体。

    在您的示例中,您尝试将 $categories 替换为 Category 实体的集合。

    阅读:CollectionType Field

    但我认为存在误解:您试图为单个帖子赋予多个类别? 你把事情反过来了吗?一个类别包含多个帖子?

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

    【讨论】:

    • 据我了解,集合类型是当您需要创建子实体的新实例时,但是当您已经创建了一组预定义的子实体时,使用 EntityType 更合适。当我为这两种情况转储form.categories.vars 时,第二种情况有一个变量choices 包含我现有的类别。关于你的问题:是的,我也有相反的一面(addPost、removePost、getPosts 和 $posts 在构造函数中初始化为 arrayColletcion)。感谢您的宝贵时间
    【解决方案2】:

    问题来自表单模板,因为我手动对其进行了编码,忘记了双括号[]。所以而不是:

    <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][]">
    

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2018-08-30
      • 2014-01-29
      相关资源
      最近更新 更多