Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用表单symfony向用户添加角色_Php_Symfony - Fatal编程技术网

Php 如何使用表单symfony向用户添加角色

Php 如何使用表单symfony向用户添加角色,php,symfony,Php,Symfony,大家好,我是symfony的新手,在尝试创建具有各自角色的用户时遇到了一点困难,每次尝试添加一个用户时,我都会遇到以下错误:类中的属性“roles”…\Entity\Usuarios“可以用方法“addRole()”、“removatorole()”定义,但新值必须是数组和/或instance\Traversable,“..\Entity\Roles”给定。 my db中的角色是ROLE_ADMIN和ROLE_USER,这都很好,但无法在表单中注册用户的角色 这是我的Usuarios.php &

大家好,我是symfony的新手,在尝试创建具有各自角色的用户时遇到了一点困难,每次尝试添加一个用户时,我都会遇到以下错误:类中的属性“roles”…\Entity\Usuarios“可以用方法“addRole()”、“removatorole()”定义,但新值必须是数组和/或instance\Traversable,“..\Entity\Roles”给定。

my db中的角色是ROLE_ADMIN和ROLE_USER,这都很好,但无法在表单中注册用户的角色

这是我的Usuarios.php

<?php

namespace Paginas\UsuariosBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
* Usuarios
*
* @ORM\Table(name="usuarios")
*    @ORM\Entity(repositoryClass="Paginas\UsuariosBundle\Repository\UsuariosRepository")
*/
class Usuarios implements AdvancedUserInterface, \Serializable
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nombre", type="string", length=200)
 */
private $nombre;

/**
 * @var string
 *
 * @ORM\Column(name="direccion", type="string", length=255)
 */
private $direccion;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="fechaNac", type="date")
 */
private $fechaNac;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=25)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=32)
 */
private $salt;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=64)
 */
private $password;

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

/**
 * @var bool
 *
 * @ORM\Column(name="estado", type="boolean")
 */
private $estado;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set nombre
 *
 * @param string $nombre
 * @return Usuarios
 */
public function setNombre($nombre)
{
    $this->nombre = $nombre;

    return $this;
}

/**
 * Get nombre
 *
 * @return string 
 */
public function getNombre()
{
    return $this->nombre;
}

/**
 * Set direccion
 *
 * @param string $direccion
 * @return Usuarios
 */
public function setDireccion($direccion)
{
    $this->direccion = $direccion;

    return $this;
}

/**
 * Get direccion
 *
 * @return string 
 */
public function getDireccion()
{
    return $this->direccion;
}

/**
 * Set fechaNac
 *
 * @param \DateTime $fechaNac
 * @return Usuarios
 */
public function setFechaNac($fechaNac)
{
    $this->fechaNac = $fechaNac;

    return $this;
}

/**
 * Get fechaNac
 *
 * @return \DateTime 
 */
public function getFechaNac()
{
    return $this->fechaNac;
}

/**
 * Set username
 *
 * @param string $username
 * @return Usuarios
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string 
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set salt
 *
 * @param string $salt
 * @return Usuarios
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string 
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * Set password
 *
 * @param string $password
 * @return Usuarios
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set email
 *
 * @param string $email
 * @return Usuarios
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set estado
 *
 * @param boolean $estado
 * @return Usuarios
 */
public function setEstado($estado)
{
    $this->estado = $estado;

    return $this;
}

/**
 * Get estado
 *
 * @return boolean 
 */
public function getEstado()
{
    return $this->estado;
}

/**
 * @ORM\ManyToMany(targetEntity="Roles", inversedBy="users", cascade={"all"});
 *
 */
private $roles;

public function __construc(){
$this->roles = new ArrayCollection();
}

/**
 * Add roles
 *
 * @param \Paginas\UsuariosBundle\Entity\Roles $roles
 * @return Usuarios
 */
public function addRole(\Paginas\UsuariosBundle\Entity\Roles $roles)
{
    $this->roles[] = $roles;    
}

/**
 * Remove roles
 *
 * @param \Paginas\UsuariosBundle\Entity\Roles $roles
 */
public function removeRole(\Paginas\UsuariosBundle\Entity\Roles $roles)
{
    $this->roles->removeElement($roles);
}

/**
 * Get roles
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getRoles()
{   
return $this->roles;    
//return $this->roles->toArray();
//return array('ROLE_ADMIN');
//return array($this->roles);   
}

/**
 * @inheritDoc
 */
public function eraseCredentials(){

}

/**
 * @see \Serializable::serialize()
 */
public function serialize(){
    return serialize(array(
    $this->id,
));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized){
    list(
   $this->id,
) = unserialize($serialized);
}

public function isAccountNonExpired()
{
return true;
}

public function isAccountNonLocked()
{
return true;
}

public function isCredentialsNonExpired()
{
return true;
}

public function isEnabled()
{
return $this->estado;
}

}

不知道该怎么办,有人能帮我吗?

您需要传递角色数组,而不是
角色
对象。在表单生成器中使用带有“角色”的
'multiple'=>true

UsuariosType.php

->add('roles', EntityType::class, array(
    'class'=> 'PaginasUsuariosBundle:Roles',
    'multiple' => true, // Allow multiple selection
    'choice_label'=>'nombre')
);

您需要传递角色数组,而不是
角色
对象。在表单生成器中使用带有“角色”的
'multiple'=>true

UsuariosType.php

->add('roles', EntityType::class, array(
    'class'=> 'PaginasUsuariosBundle:Roles',
    'multiple' => true, // Allow multiple selection
    'choice_label'=>'nombre')
);

在/app/config/security.yml中定义您的角色。如果需要,您还可以添加以下角色:

role_hierarchy:
    ROLE_ADMIN:         [ROLE_ADMIN]
    ROLE_SUPER_ADMIN:   [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    ROLE_TEACHER:       [ROLE_TEACHER]
    ROLE_STUDENT:       [ROLE_STUDENT]
    ROLE_PARENT:        [ROLE_PARENT]
如果您需要在控制器中获得角色

$roles = $this->getParameter('security.role_hierarchy.roles');
如果需要将角色放入FormType中

$roles = $this->getParent('security.role_hierarchy.roles');
在表格中,(注意这是一个多选的示例,您可以将其更改为下拉列表、复选框或您想要的)


在/app/config/security.yml中定义您的角色。如果需要,您还可以添加以下角色:

role_hierarchy:
    ROLE_ADMIN:         [ROLE_ADMIN]
    ROLE_SUPER_ADMIN:   [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    ROLE_TEACHER:       [ROLE_TEACHER]
    ROLE_STUDENT:       [ROLE_STUDENT]
    ROLE_PARENT:        [ROLE_PARENT]
如果您需要在控制器中获得角色

$roles = $this->getParameter('security.role_hierarchy.roles');
如果需要将角色放入FormType中

$roles = $this->getParent('security.role_hierarchy.roles');
在表格中,(注意这是一个多选的示例,您可以将其更改为下拉列表、复选框或您想要的)


角色表单字段应为集合,而不是单个实体。此外:用户实体和角色实体中的构造函数命名错误(缺少t,
\u construc()
)使用用户管理是一个很好的选择。您的角色表单字段应该是一个集合而不是单个实体。此外,您的用户实体和角色实体中的构造函数被命名为错误(缺少T、<代码>使用用户管理是一个很好的选择。请将答案标记为“答案”。也可以添加您的树枝吗?Gracias!请标记为答案。也可以添加您的枝条吗?