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

筅森魡賤 提交于 2019-12-23 04:50:54

问题


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 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> 

The problem:

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

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.

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


}

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

}

回答1:


Your field 'categories' need to be a CollectionType of entity and not just a entity.

In your example you try to replace $categories which is a collection by a Category entity.

Read this : CollectionType Field

But I was of the opinion that there is a misunderstanding: You try to give multiple categories to a single post? Have you reversed the thing? One Category contains multiple Posts ?

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




回答2:


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

I should have been using:

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

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

Thanks



来源:https://stackoverflow.com/questions/39270448/error-the-new-value-must-be-an-array-or-an-instance-of-traversable-entity-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!