gpt4 book ai didi

forms - symfony2 形成多选与 arraycollection

转载 作者:行者123 更新时间:2023-12-01 10:54:09 28 4
gpt4 key购买 nike

我想创建一个表单来编辑我的用户。与 ManyToMany 关联的用户和角色。在 UserUsers 实体中,我有一个 $roles 变量,它是 ArrayCollection:

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 ) );
}

现在我的多选是空的。

如果我将 mapped 设置为 true,我会收到一条错误消息:

UserRoles could not be converted to int in...

我试了很多方法,都没有正确解决这个问题。

最佳答案

对于实体的选择,您应该使用特殊的选择字段类型“实体”(参见 Symfony 手册中的 entity field type)。例如,请参阅我对 similar question 的回答.如果您遇到更多错误,您也可以在 Role Interface and Manage Roles 上找到这个问题。有帮助。

关于forms - symfony2 形成多选与 arraycollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com