2012-12-17 5 views
-1

로그인 할 때 사용자 ROL를 검색 할 수 없습니다이 내 사용자 개체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; 
    } 
} 

제발, 내가 뭘 잘못하고 있는지 모르겠어?

나는 ROLES를 관리해야하는데, 그 이유는 내가 엔티티를 가지고 있기 때문이다. 된 Rol 엔티티에서

ROL ROLE_ADMIN이, ROLE_USER가 ... denominacion이 ... 내가 반환해야 Usuario에 ROL

답변

1

기능 getRoles()을 retreive하는 등 슈퍼 관리자, 관리자, 사용자, 필요되어있다 역할 이름은 array입니다. 왜 네가 비어 있니?

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

Usuario은 역할과 많은 관계가 있습니다. 이게 옳은 거니? 위의 예에서는 역할 모음이 있다고 가정합니다.

+0

안녕하세요,이 코드를 추가하면 알림 : 정의되지 않은 속성 : Monse \ UsuariosBundle \ Entity \ Usuario :: $ 역할을 받으실 수 있습니다. – Splendonia

+0

역할 테이블의 행은 다음과 같습니다. (id rol denominacion) -> 2 ROLE_ADMIN Administrador 및 Users 테이블의 행은 다음과 같습니다. (id cliente_id rol (id 참조) usuario 전자 메일 비밀 번호 salt last_login sign_up_date) -> 6 2 1 sara [email protected] 여기에 암호가 암호화되었습니다. 2012-12-17 2012-12-17 – Splendonia

+0

나는 많은 관계를 정의했는데 정의 된 역할 계층도 있습니다. 내가 잘못했는지 알려주세요. – Splendonia

관련 문제