我知道有类似的问题,但没有一个解决方案适用于Symfony 3.x。
所以我有三个实体,用户,角色和权限。用户具有角色(ManyToMany)和权限(也是ManyToMany)。然后我有一个表单,虽然Permissions工作正常,但Roles却没有。
我能看到的唯一区别是,返回角色的函数不应该被称为getRoles(?),因为具有此名称的函数必须返回字符串数组(不是角色实体数组)满足AdvancedUserInterface的期望,这就是为什么另一个函数(getRoleEntities)返回$ this->角色,但我相信我在构建表单时反映了它。
以下是User实体类的缩短版本:
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable {
//id, username, personalname, password, email and isactive ommited
/**
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Permission", cascade = {"persist"})
* @ORM\JoinTable(name="user_permission")
*/
private $permissions;//this works
/**
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Role", cascade = {"persist"})
* @ORM\JoinTable(name="user_role")
*/
private $roles;//this doesn't work
public function __construct() {
$this->permission = new ArrayCollection();
$this->roles = new ArrayCollection();
}
//irrelevant getters/setters ommited
/**
* Add Permission
*
* @param \AppBundle\Entity\Permission $userPermission
*
* @return User
*/
public function addPermission(\AppBundle\Entity\Permission $userPermission) {
$this->permissions[] = $userPermission;
return $this;
}
/**
* Remove Permission
*
* @param \AppBundle\Entity\Permission $userPermission
*/
public function removePermission(\AppBundle\Entity\Permission $userPermission) {
$this->permissions->removeElement($userPermission);
}
/**
* Get Permissions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPermissions() {
return $this->permissions;
}
////////////////////////////////
//roles
/**
* Add role
* @param \AppBundle\Entity\Role $role
*
* @return User
*/
public function addRoleEntity(\AppBundle\Entity\Role $role) {
$this->roles[] = $role;
return $this;
}
/**
* Remove role
* @param \AppBundle\Entity\Role $role
*/
public function removeRoleEntity(\AppBundle\Entity\Role $role) {
$this->roleRoles->removeElement($role);
}
/**
* I know this one should simply return $this->roles, but it has to return array of strings to meet expectations of the AdvancedUserInterface, that is why another function (getRoleEntities) returns $this->roles
* @return array
*/
public function getRoles() {
$ret_val = array();
$roles = $this->getRoleEntities();
if ($roles) {
foreach ($roles as $role) {
$ret_val[] = $role->getRoleName();
}
}
return $ret_val;
}
/**
* Get roles
*/
public function getRoleEntities() {
return $this->roles;
}
}
以下是我如何构建表单:
$em = $this->getDoctrine()->getManager();
$user = $em->find('AppBundle:User',1);//just for testing
$form = $this->createFormBuilder($user)
->add('personal_name', \Symfony\Component\Form\Extension\Core\Type\TextType::class, array('label' => 'Imię i nazwisko: '))
->add('is_active', \Symfony\Component\Form\Extension\Core\Type\CheckboxType::class, array('label' => 'Active: ','required' => false))
->add('permissions', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class , array(
'class' => 'AppBundle\Entity\Permission',
'multiple' => true,
'expanded' => true
))
->add('roleEntities', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class , array(
'class' => 'AppBundle\Entity\Role',
))
->add('save', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array('label' => 'Save'))
->getForm();
当我打开一个页面时,我得到了这个例外:
属性" roleEntities"也不是其中一种方法 " addRoleEntity()" /" removeRoleEntity()"," setRoleEntities()", " roleEntities()"," __ set()"或" __ call()"存在并具有公共访问权限 在课堂上#34; AppBundle \ Entity \ User"。
PS。我标记了这个" symfony2",因为AFAIK在2.9和3.0之间没有太大差异
答案 0 :(得分:0)
好吧,幸运的是来自#Symfony irc的人帮帮我,那里有一些问题:
应该在变量之后调用类似addRoleEntity的函数,例如addRole
像@malcom提到的那样,在构建表单时,而不是:
- >添加(' roleEntities',\ Symfony \ Bridge \ Doctrine \ Form \ Type \ EntityType :: class,array( '类' => ' AppBundle \ Entity \ Role',
应该是:
->add('roles', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class , array(
'class' => 'AppBundle\Entity\Role',
'multiple' => true,
)
因为这是变量的命名方式。另外,由于这是ManyToMany,我还添加了多个' =>是的,否则它确实保存了,但却无法加载这些信息。
3.它更进一步,出现了一个新错误:无法从选择列表中读取选项。
我将getRoles方法的主体更改为:
return $this->roles->toArray();
它有效。
最后一个想法是我的,而不是来自IRC,我这样说,因为我不喜欢这个解决方案。如果这是一个糟糕的解决方案,那就是我的错。