0

这是我的用户实体

<?php

namespace Monse\UsuariosBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * Monse\UsuariosBundle\Entity\Usuario
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class Usuario implements UserInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** 
 * @ORM\OneToOne(targetEntity="Monse\ClientesBundle\Entity\Cliente")
 * @ORM\JoinColumn(name="cliente_id", referencedColumnName="id",nullable=true)
*/
    protected $cliente;

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

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

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

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

    /**
     * @var date $ultimo_ingreso
     *
     * @ORM\Column(name="ultimo_ingreso", type="date")
     */
    protected $ultimo_ingreso;

    /**
     * @var date $fecha_alta
     *
     * @ORM\Column(name="fecha_alta", type="date")
     */
    protected $fecha_alta;

     /**
* @ORM\ManyToOne(targetEntity="Monse\UsuariosBundle\Entity\Rol")
* @ORM\JoinColumn(name="rol", referencedColumnName="id",nullable=false)
*/
    protected $rol;


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

public function setCliente(\Monse\ClientesBundle\Entity\Cliente $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(\Monse\UsuariosBundle\Entity\Rol $rol)
    {
        $this->rol = $rol;
    }

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

    public function __construct()
    {
        $this->fecha_alta = new \DateTime();
    }

    function equals(\Symfony\Component\Security\Core\User\UserInterface $usuario)
    {
        return $this->getUsuario() == $usuario->getUsername();
    }

    function eraseCredentials()
    {
    }

    function getRoles()
    {
    }

    function getUsername()
    {
        return $this->getUsuario();
    }
}

这是我的角色实体

    <?php

namespace Monse\UsuariosBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\ORM\Mapping as ORM;
/**
 * Monse\UsuariosBundle\Entity\Rol
 *
 * @ORM\Table(name="roles")
 * @ORM\Entity
 */
class Rol implements RoleInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $role
     *
     * @ORM\Column(name="rol", type="string", length=255)
     */
    protected $role;

    /**
     * @var string $denominacion
     *
     * @ORM\Column(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

4

1 回答 1

1

函数getRoles()inUsuario应该返回一个array角色名称。为什么你的是空的?

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

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

于 2012-12-18T04:11:08.633 回答