Symfony2 - 用户通过 id 引用角色实体,但我在登录时无法检索用户 rol



这是我的用户实体

<?php
namespace MonseUsuariosBundleEntity;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineORMMapping as ORM;
/**
 * MonseUsuariosBundleEntityUsuario
 *
 * @ORMTable(name="users")
 * @ORMEntity
 */
class Usuario implements UserInterface
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /** 
 * @ORMOneToOne(targetEntity="MonseClientesBundleEntityCliente")
 * @ORMJoinColumn(name="cliente_id", referencedColumnName="id",nullable=true)
*/
    protected $cliente;
    /**
     * @var string $usuario
     *
     * @ORMColumn(name="usuario", type="string", length=255, unique=true)
     */
    protected $usuario;
    /**
     * @var string $email
     *
     * @ORMColumn(name="email", type="string", length=255, unique=true)
     */
    protected $email;
    /**
     * @var string $password
     *
     * @ORMColumn(name="password", type="string", length=255)
     */
    protected $password;
    /**
     * @var string $salt
     *
     * @ORMColumn(name="salt", type="string", length=255)
     */
    protected $salt;
    /**
     * @var date $ultimo_ingreso
     *
     * @ORMColumn(name="ultimo_ingreso", type="date")
     */
    protected $ultimo_ingreso;
    /**
     * @var date $fecha_alta
     *
     * @ORMColumn(name="fecha_alta", type="date")
     */
    protected $fecha_alta;
     /**
* @ORMManyToOne(targetEntity="MonseUsuariosBundleEntityRol")
* @ORMJoinColumn(name="rol", referencedColumnName="id",nullable=false)
*/
    protected $rol;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
public function setCliente(MonseClientesBundleEntityCliente $cliente)
    {
        $this->cliente = $cliente;
    }
    /**
     * Get cliente
     *
     * @return integer 
     */
    public function getCliente()
    {
        return $this->cliente;
    }
    /**
     * Set usuario
     *
     * @param string $usuario
     */
    public function setUsuario($usuario)
    {
        $this->usuario = $usuario;
    }
    /**
     * Get usuario
     *
     * @return string 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }
    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }
    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }
    /**
     * Set contrasena
     *
     * @param string $contrasena
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }
    /**
     * Get contrasena
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }
    /**
     * Set salt
     *
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }
    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }
    /**
     * Set ultimo_ingreso
     *
     * @param date $ultimoIngreso
     */
    public function setUltimoIngreso($ultimoIngreso)
    {
        $this->ultimo_ingreso = $ultimoIngreso;
    }
    /**
     * Get ultimo_ingreso
     *
     * @return date 
     */
    public function getUltimoIngreso()
    {
        return $this->ultimo_ingreso;
    }
    /**
     * Set fecha_alta
     *
     * @param date $fechaAlta
     */
    public function setFechaAlta($fechaAlta)
    {
        $this->fecha_alta = $fechaAlta;
    }
    /**
     * Get fecha_alta
     *
     * @return date 
     */
    public function getFechaAlta()
    {
        return $this->fecha_alta;
    }
    /**
     * Set rol
     *
     * @param integer $rol
     */
    public function setRol(MonseUsuariosBundleEntityRol $rol)
    {
        $this->rol = $rol;
    }
    /**
     * Get rol
     *
     * @return integer 
     */
    public function getRol()
    {
        return $this->rol;
    }
    public function __construct()
    {
        $this->fecha_alta = new DateTime();
    }
    function equals(SymfonyComponentSecurityCoreUserUserInterface $usuario)
    {
        return $this->getUsuario() == $usuario->getUsername();
    }
    function eraseCredentials()
    {
    }
    function getRoles()
    {
    }
    function getUsername()
    {
        return $this->getUsuario();
    }
}

这是我的角色实体

    <?php
namespace MonseUsuariosBundleEntity;
use SymfonyComponentSecurityCoreRoleRoleInterface;
use DoctrineORMMapping as ORM;
/**
 * MonseUsuariosBundleEntityRol
 *
 * @ORMTable(name="roles")
 * @ORMEntity
 */
class Rol implements RoleInterface
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string $role
     *
     * @ORMColumn(name="rol", type="string", length=255)
     */
    protected $role;
    /**
     * @var string $denominacion
     *
     * @ORMColumn(name="denominacion", type="string", length=255)
     */
    protected $denominacion;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set rol
     *
     * @param string $rol
     */
    public function setRole($role)
    {
        $this->role = $role;
    }
    /**
     * Get rol
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }
     public function setDenominacion($denominacion)
    {
        $this->denominacion = $denominacion;
    }
    /**
     * Get denominacion
     *
     * @return string 
     */
    public function getDenominacion()
    {
        return $this->denominacion;
    }
    public function __toString() {
        return $this->denominacion;
    }
}

请帮忙,我不知道我做错了什么?

我需要管理角色,这就是我拥有实体的原因。

在 Rol 实体

罗尔有ROLE_ADMIN,ROLE_USER...名称是超级管理员,管理员,用户等...我需要检索 ROL

Usuario 中的函数getRoles()应返回角色名称的array。为什么你的是空的?

public function getRoles()
{
    return $this->roles->map(function($r) { return $r->getRole(); })
        ->toArray();
}

Usuario与角色有着多对一的关系。你确定这是正确的吗?上面的示例假定您有一个角色集合。

最新更新