【发布时间】:2017-01-15 06:13:53
【问题描述】:
您好,我是 Symfony 2 的新手。到现在我已经处理了几个小时这个问题,所以我不得不向社区提问。
我有两个实体
用户 --> 语言 (OneToMany)
用户实体
/**
*
* @ORM\OneToMany(targetEntity="User_Language", mappedBy="user")
*/
private $languages;
public function __construct() {
$this->languages = new ArrayCollection();
}
User_Language 实体
/** @ORM\Column(type="string", length=50) */
private $language;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="languages")
*/
private $user;
User_LanguageTyp
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add( 'language' );
}
我正在尝试构建一个表单,用户可以在其中添加/编辑/删除他的语言
数据库中的用户语言
从数据库(在控制器中)获取数据时 - 使用 $user->GetLanguages() 我得到了persistantCollection, 使用 QueryBuilder() 我得到了一个数组
有什么办法,如何将它传递给 CreateForm() 函数?
$form = $this->createForm( User_LanguageType::class, $user->getLanguages() );
我遇到了这两个错误:
The form's view data is expected to be an instance of class ArpanetRuzicja7EstatesBundle\Entity\User_Language, but is an instance of class Doctrine\ORM\PersistentCollection.
或
The form's view data is expected to be an instance of class ArpanetRuzicja7EstatesBundle\Entity\User_Language, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of ArpanetRuzicja7EstatesBundle\Entity\User_Language.
设置'data_class' => null,没有帮助。
非常感谢。
【问题讨论】:
标签: php forms symfony collections