简体   繁体   English

Symfony2错误提交表单与实体类型(多对多关系)

[英]Symfony2 error submitting form with entity type (many to many relation)

I created a simple Many to Many ($post --> $category) relation in symfony2, which I want to use in my form builder thanks to the "entity" field type, with a multiple = false option (default) for now, eventhough it's many to many. 我在symfony2中创建了一个简单的多对多($ post - > $ category)关系,我想在我的表单构建器中使用,这要归功于“实体”字段类型,现在有一个multiple = false选项(默认),尽管它有很多很多。

This is part of my Post entity : 这是我的Post实体的一部分:

 /**
 * @var ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="Yeomi\PostBundle\Entity\Category", inversedBy="posts")
 */
private $categories;



/**
 * Add categories
 *
 * @param \Yeomi\PostBundle\Entity\Category $categories
 * @return Post
 */
public function addCategory(\Yeomi\PostBundle\Entity\Category $category)
{
    $this->categories[] = $category;
    return $this;
}

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


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

This is my PostType : 这是我的PostType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text')
        ->add('content', 'textarea')
        ->add('published', 'checkbox')
        ->add("categories", "entity", array(
            "class" => "YeomiPostBundle:Category",
            "property" => "name",
            "multiple" => false,
        ))
        ->add('published', 'checkbox')
        ->add('save', "submit")
    ;
}

And this is the error I get 这就是我得到的错误

500 Internal Server Error - NoSuchPropertyException 500内部服务器错误 - NoSuchPropertyException

Neither the property "categories" nor one of the methods "addCategory()"/"removeCategory()", "setCategories()", "categories()", "__set()" or "__call()" exist and have public access in class "Yeomi\PostBundle\Entity\Post". 

It works with multiple = true, but that's not what I want, would it be because the relation is many to many so I am forced to make it a multiple entity field ? 它适用于multiple = true,但这不是我想要的,是不是因为关系是多对多所以我被迫使它成为一个多实体字段?

I've cleared cache, doctrine cache, I've regenerate my entity getter/setter, don't know what I may be doing wrong.. 我已经清除缓存,doctrine缓存,我重新生成了我的实体getter / setter,不知道我可能做错了什么..

Thanks for your help 谢谢你的帮助

Found the answer thanks to symfony2 Entity select tag display 通过symfony2实体选择标签显示找到答案

The issue was indeed because of the ManyToMany relation, (this actually makes sense, because if you have a ManyToMany relation, you want to be able to select multiple entities), 问题确实是因为ManyToMany关系,(这实际上是有道理的,因为如果你有一个ManyToMany关系,你希望能够选择多个实体),

You can get this to work with a multiple = false by adding a setter for the whole ArrayCollection property. 通过为整个ArrayCollection属性添加setter,可以使用multiple = false来实现此操作。

/**
 * Set categories
 * @param \Doctrine\Common\Collections\Collection $categories
 *
 * @return Post
 */
public function setCategories($categories)
{

    if(!is_array($categories))
    {
        $categories = array($categories);
    }
    $this->categories = $categories;

    return $this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM