【问题标题】:symfony2 many-to-many form checkboxsymfony2 多对多表单复选框
【发布时间】:2020-09-19 06:02:54
【问题描述】:

我在 symfony 中创建了 2 个实体:多对多关系中的用户和角色。这意味着每个用户都可以拥有更多角色,并且可以为多个用户设置角色。

用户类别:

  /**
   * @ORM\Entity
   * @ORM\Table(name="JEP_User")
   * @ORM\Entity(repositoryClass="Chrchel\JepBundle\Repository\UserRepository")
   */
class User implements AdvancedUserInterface {

/**
 * @ORM\Id()
 * @ORM\Column(name="id",type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="username",type="string",length=100,unique=true)
 */
private $username;

 /**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
 *
 */
protected $roles;

//....
}

角色类:

/**
 * @ORM\Table(name="JEP_Role")
 * @ORM\Entity()
 */
 class Role implements RoleInterface {

/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

  /** @ORM\Column(name="name", type="string", length=30) */
protected $name;

 /** @ORM\Column(name="role", type="string", length=20, unique=true) */
protected $role;

 /** @ORM\ManyToMany(targetEntity="User", mappedBy="roles") */
protected $users;
 //...
 }

我不知道如何在 Symfony2 中编写查询构建器来列出所有存在的角色并将其添加到 UserForm 的末尾,在那里可以选择(作为复选框)授予用户的角色。 我尝试在 UserType 中使用这样的集合

->add('roles', 'collection',array('label' => 'Role', 'required' => false,'type'=> new RoleType()))

我从 symfony 得到的最好的东西是带有选定角色名称的文本框的行。但这不是我需要的。

【问题讨论】:

    标签: forms symfony many-to-many


    【解决方案1】:

    我使用了实体类型而不是集合。 I 事物集合主要用于实际创建一个Role 对象并将其分配给User

    如果您只想列出所有现有角色并能够选择并将其分配给用户,那么:

    ->add('roles', 'entity', array(
        'class' => 'MyBundle:Role',
        'property'     => 'name',
        'multiple'     => true
    ));
    

    编辑:这会将小部件呈现为多个<select>,参考entity type 以呈现为复选框列表。

    【讨论】:

    • expanded 选项设置为true 以获得复选框列表。并且您需要在Role 模型上实现__toString() 方法(用于在每个复选框旁边显示标签。
    • 我发现我必须向 User 实体添加另一种方法才能将角色作为集合而不是数组返回 - getRolesAsCollection。
    • 你如何称呼这个特定的getRolesAsCollection 以及在哪里?
    【解决方案2】:

    Symfony3:

    如果有人在使用 Symfony3

    use Symfony\Bridge\Doctrine\Form\Type\EntityType;
    
    ->add('roles', EntityType::class, array( // <-- EntityType::class is unique to Symfony3
        'class' => 'AppBundle:Role',
        'choice_label' => 'name', // <-- choice_label is unique to Symfony3
        'multiple' => true
    ))
    

    【讨论】:

    • 我的格式与上面完全相同(Symfony 3:答案 4)。由“角色”表示的字段已设置为多对多。我使用 ArrayCollection() 设置了它(也试过没有)。无论哪种方式,我都会遇到错误,并且我一生都无法弄清楚。希望得到一些反馈。没有 ArrayCollection:错误 =“无法转换属性路径“用户”的值:需要一个 Doctrine\Common\Collections\Collection 对象。”
    【解决方案3】:

    @user1041880:如果你使用 symfony 安全函数(需要将 euser 的角色作为一个数组),你可以这样做:

    ->add('rolesAsCollection', 'entity', array(
        'class' => 'MyBundle:Role',
        'property'     => 'name',
        'multiple'     => true
    ));
    

    在你的用户类中:

    public function getRolesAsCollection()
    {
        return $this->roles;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2019-07-24
      • 2014-07-09
      • 1970-01-01
      • 2012-10-28
      • 2012-11-27
      相关资源
      最近更新 更多