Symfony 3,ManyToOne形式的关系

时间:2017-10-07 17:50:36

标签: php symfony doctrine relational-database symfony-3.2

我有几个实体,我尝试设置用户和权限之间的关系。我希望有可能为用户分配权限组,因此我创建了实体User,GroupAssociationsUser,Permissions。我认为最好的方法是在GroupAssociationsUser中为用户和权限创建关系ManyToOne。您可以看到GroupAssociationsUser实体的代码:

/**
 * @ORM\ManyToOne(targetEntity="Permissions", inversedBy="group_assoc_user")
 * @ORM\JoinColumn(name="group_permissions", referencedColumnName="id", nullable=FALSE)
 */
private $group_permissions;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="group_assoc_user")
 * @ORM\JoinColumn(name="user_assoc", referencedColumnName="id", nullable=FALSE)
 */
private $user_assoc;

然后我还在用户实体中创建了关系:

/**
 * @ORM\OneToMany(targetEntity="GroupAssociationsUser", mappedBy="user_assoc")
 */
private $group_assoc_user;


public function __construct()
{
    $this->group_assoc_user = new ArrayCollection();
}

最后在Permission实体中:

/**
* @ORM\OneToMany(targetEntity="GroupAssociationsUser", mappedBy="group_permissions")
*/
private $group_assoc_user;

public function __construct()
{
$this->group_assoc_user = new ArrayCollection();
}

所以基本上我想在我的用户编辑页面上选择列表,我希望有可能选择要分配给用户的权限组。

所以我用:

创建了表单
->add('group_assoc_user', EntityType::class, array(
            'class' => 'AppBundle:Permissions',
            'choice_label'=> 'group_name',
            'placeholder' => 'no rights',
        ))

问题在于,无论我做什么,我只有现有权限列表,或者如果我将代码更改为:

        ->add('group_assoc_user', EntityType::class, array(
            'class' => 'AppBundle:GroupAssociationsUser',
            'choice_label'=> 'Permissions.group_name',
            'placeholder' => 'no rights',
        ));

我只列出了未分配的分配权限。我究竟做错了什么?

即使我获得了所有权限名称,我如何强制学说标记已分配的字段?

1 个答案:

答案 0 :(得分:1)

尝试将Multiple设置为true。

http://symfony.com/doc/current/reference/forms/types/entity.html#multiple

    ->add('group_assoc_user', EntityType::class, array(
        'class' => 'AppBundle:GroupAssociationsUser',
        'choice_label'=> 'Permissions.group_name',
        'placeholder' => 'no rights',
        'multiple' => true,
    ));

您可能也喜欢

'expanded' => true

选项