我想創建一個窗體來編輯我的用戶。 用戶和角色ManyToMany。 在UserUsers實體我有一個$角色變量,它是ArrayCollection
:symfony2使用arraycollection形式多選select
public function __construct()
{
$this->roles = new ArrayCollection();
}
在我的形式,我想通過多選表單元素將角色添加到我的用戶。 在我的用戶形式:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username')
->add('password', 'repeated', array(
'type' => 'password',
'mapped' => false,
'required' => false,
'first_options' => array(
'label' => 'Password'),
'second_options' => array(
'label' => 'Repeat Password')))
->add('roles', 'choice', array(
'mapped' => false,
'multiple' => true));
}
現在我的多個選擇是空的。
如果我把映射到真實的,我得到一個錯誤信息:
UserRoles could not be converted to int in...
我已經嘗試了很多方法,但我不能正確地解決這個問題。
你在$ roles ArrayCollection中有什麼類型? – redbirdo 2013-05-09 13:11:47
$ roles中的所有元素ArrayCollection是一個實體(UserRoles)。 – lordjancso 2013-05-09 14:26:49
對於實體的選擇,你應該使用專用的選擇字段類型「實體」(http://symfony.com/doc/current/reference/forms/types/entity.html)。對於一個例子看到我的答案類似的問題 - http://stackoverflow.com/questions/13519961/symfony-2-form-create-user-and-add-group/13521462#13521462 – redbirdo 2013-05-09 15:50:08