Symfony2 - потребител препраща към обект с роли по id, но не мога да извлека ролята на потребителя, когато вляза

Това е моят потребителски обект

<?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

rol има ROLE_ADMIN, ROLE_USER... деноминацията е Super Admin, Admin, User и т.н.... Трябва да изтегля ROL


person Splendonia    schedule 17.12.2012    source източник


Отговори (1)


Функция getRoles() в Usuario трябва да върне array имена на роли. Защо вашият е празен?

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

Usuario има връзка много към едно с ролята. Сигурни ли сте, че това е правилно? Горният пример предполага, че имате колекция от роли.

person gremo    schedule 18.12.2012
comment
Здравейте, празен е, защото ако добавя този код, получавам Забележка: Недефинирано свойство: Monse\UsuariosBundle\Entity\Usuario::$roles на този ред. - person Splendonia; 18.12.2012
comment
Ред в таблицата с роли изглежда така :(id rol denominacion) -› 2 ROLE_ADMIN Administrador и ред в таблицата Users изглежда така: (id cliente_id rol (посочен от id) usuario имейл парола сол last_login sign_up_date) -› 6 2 1 sara [email protected] шифрована парола тук шифрована сол тук 2012-12-17 2012-12-17 - person Splendonia; 18.12.2012
comment
Дефинирах връзката като много към едно, също така имам дефинирана йерархия на ролите. Моля, уведомете ме, ако го правя погрешно. - person Splendonia; 18.12.2012
comment
Опитайте return array($this->rol->getRole()); - person gremo; 18.12.2012