[英]Symfony form wrong property mapping in enities
我已經在注冊表格(FOSUserBundle)中實現了組選擇。 提交表單后,Symfony將錯誤映射我的類型。 目的是通過注冊表單將所選組添加到用戶。
這是我的用戶實體:
class User extends BaseUser
{
protected $groups;
public function __construct()
{
parent::__construct();
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addGroup(\FOS\UserBundle\Model\GroupInterface $groups)
{
$this->groups[] = $groups;
return $this;
}
public function removeGroup(\FOS\UserBundle\Model\GroupInterface $groups)
{
$this->groups->removeElement($groups);
}
public function getGroups()
{
return $this->groups;
}
}
這是我的表單類型中的相關部分。
$builder->add('groups', 'entity', array(
'label' => 'Type',
'required' => true,
'class' => 'xxUserBundle:Group',
'property' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')->where('g.locked = false');
}
))
提交后,Symfony將引發以下異常。
屬性“ groups”或方法“ setGroups()”,“ _ set()”或“ _call()”都不存在,並且在類“ xx \\ UserBundle \\ Entity \\ User”中沒有公共訪問權限。
/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 376
/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php中/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 376
異常。
對於數組屬性,這意味着原則上的任何manyToXX
關系,都不會自動生成setter方法,對於數組,總是會生成一個add
方法來代替set
。 為什么Symfony沒有找到正確的方法?
臨時解決方案是add
方法
public function setGroups(\FOS\UserBundle\Model\GroupInterface $groups)
{
return $this->addGroup($groups);
}
到用戶實體。 但是我認為這不是正確的解決方案……有人知道錯誤在哪里或發生了什么?
我正在使用Symfony 2.4.1版
謝謝。
您可能在'class'=>'xxUserBundle:Group'中引用了錯誤的類,但是您顯示的實體是User實體。 這應該是用戶實體嗎?
我發現創建自己的表單類很有用,因為您可以更頻繁地重用它,而實際上,當您擁有更高級的表單(包括表單包含實體或集合的情況)時,它使調試表單變得更加容易。 http://symfony.com/doc/current/book/forms.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.