Forms symfony2多对多表单复选框
我在symfony中创建了两个实体:多对多关系中的用户和角色。这意味着每个用户都可以拥有更多的角色,并且可以将角色设置为多个用户 用户类:Forms symfony2多对多表单复选框,forms,symfony,many-to-many,Forms,Symfony,Many To Many,我在symfony中创建了两个实体:多对多关系中的用户和角色。这意味着每个用户都可以拥有更多的角色,并且可以将角色设置为多个用户 用户类: /** * @ORM\Entity * @ORM\Table(name="JEP_User") * @ORM\Entity(repositoryClass="Chrchel\JepBundle\Repository\UserRepository") */ class User implements AdvancedUserInte
/**
* @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;
//...
}
角色类别:
/**
* @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得到的最好结果是带有文本框的行,其中包含选定的角色名称。但这不是我需要的。我使用了实体类型而不是集合。I thing collection主要用于实际创建
角色
对象,并将其分配给用户
如果您只想列出所有现有角色,并能够选择并将其分配给用户,则:
->add('roles', 'entity', array(
'class' => 'MyBundle:Role',
'property' => 'name',
'multiple' => true
));
编辑:这会将小部件呈现为多个
,请参阅呈现为复选框列表。@user1041880:如果您使用symfony安全功能(需要将用户角色作为数组),您可以这样做:
->add('rolesAsCollection', 'entity', array(
'class' => 'MyBundle:Role',
'property' => 'name',
'multiple' => true
));
在您的用户类中:
public function getRolesAsCollection()
{
return $this->roles;
}
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\Bridge\doctor\Form\Type\EntityType;
->添加('roles',EntityType::class,array(//'AppBundle:Role',
'choice_label'=>'name',//true
))
将expanded
选项设置为true
以显示复选框列表。您需要在角色
模型上实现\uu toString()
方法(用于在每个复选框旁边显示标签。我发现我必须向用户实体添加另一个方法,以将角色作为集合而不是数组-getRolesAsCollection返回。您如何调用此特定的getRolesAsCollection
和何处?我的格式与上面的格式完全相同(Symfony 3:答案4)。由“角色”表示的字段已设置为多个。我使用ArrayCollection()进行了设置(也尝试了无)。无论哪种方式,我都会遇到错误,并且终生无法解决。希望得到一些反馈。如果没有ArrayCollection:error=“无法转换属性路径的值”用户“:应为条令\Common\Collections\Collection对象。”