我在symfony2中创建了一个简单的多对多($ post - > $ category)关系,我想在我的表单构建器中使用,这要归功于“entity”字段类型,带有multiple = false选项(默认值) )就目前而言,尽管它有很多甚至很多。
这是我的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;
}
这是我的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")
;
}
这是我得到的错误
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".
它适用于multiple = true,但这不是我想要的,是不是因为关系是多对多,所以我被迫使它成为一个多实体字段?
我已经清除了缓存,doctrine缓存,我重新生成了我的实体getter / setter,不知道我可能做错了什么..
感谢您的帮助