错误:新值必须是数组或实例的\Traversable,Entity类 [英] Error : the new value must be an array or an instance of \Traversable, Entity class given

查看:100
本文介绍了错误:新值必须是数组或实例的\Traversable,Entity类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 manyToMany 关联 Post 类别。我试图提交邮政表格,从下拉列表< select> 中创建一个包含所选类别的信息。

I have a manyToMany association between Post and Category. I am trying to submit the Post form to create a post with the selected category from the dropdown <select>.

Form正在渲染相关性:

Form is rendering correclty using:

<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天时间):

When I click on submit button I have the following error(for which I have spent about 2 days trying to figure out):


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

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.

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

Here are my form type and entity (if ever they are useful). I thank you in advance for your invaluable time and help as usual.

实体发布:

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

}


推荐答案

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

The problem were coming from the form template because I manually coded it forgetting double brackets []. So instead of:

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

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

I hope other person don't make the same mistake.

谢谢

这篇关于错误:新值必须是数组或实例的\Traversable,Entity类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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