0

Me gustaria comentaros un problema que tengo en a ver si me podeis ayudar Tengo una tabla USER donde se almacenan los distintos tipos de usuarios, cada uno con su correspondiente ROL (ROLE_ADMIN, ROLE_PROFE, ROLE_ALUMNO) Intento realizar un SELECT en para que solo aparezcan aquellos que tengan el ROLE_PROFE,

public function ProfesoresTotal($rol)
{
return $this->createQueryBuilder(‘u’)
->select(‘u’)
->where(‘u.roles = :rol’)
->setParameter(‘rol’,$rol)
->getQuery()->execute();
}

En la variable $rol que paso por parametro ya le hecho de todo e usado string normales, e usado json_encode y json_decode sin resultado, imagino que en $rol debe de ser de tipo json o contenerlo y ya no se que hacer En mi bdd hecha en la tabla roles guarda el rol asi: [“ROLE_PROFE”]

Mi entidad User

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\Entity(repositoryClass=”App\Repository\UserRepository”)
* @UniqueEntity(fields={“email”}, message=”There is already an account with this email”)
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type=”integer”)
*/
private $id;

/**
* @ORM\Column(type=”string”, length=180, unique=true)
*/
private $email;

/**
* @ORM\Column(type=”json”)
*/
private $roles = [];

/**
* @var string The hashed password
* @ORM\Column(type=”string”)
*/
private $password;

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity=”Cursos”, inversedBy=”user”)
* @ORM\JoinTable(name=”cursos_has_user”,
* joinColumns={
* @ORM\JoinColumn(name=”user_id”, referencedColumnName=”id”)
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name=”cursos_idCurso”, referencedColumnName=”idCurso”)
* }
* )
*/
private $cursoscurso;

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

/**
* Constructor
*/
public function __construct()
{
$this->cursoscurso = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(string $email): self
{
$this->email = $email;

return $this;
}

/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}

/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = ‘ROLE_ALUMNO’;

return array_unique($roles);
}

public function setRoles(array $roles): self
{
$this->roles = $roles;

return $this;
}

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

public function setPassword(string $password): self
{
$this->password = $password;

return $this;
}

/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the “bcrypt” algorithm in security.yaml
}

/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}

/**
* @return Collection|Cursos[]
*/
public function getCursoscurso(): Collection
{
return $this->cursoscurso;
}

public function addCursoscurso(Cursos $cursoscurso): self
{
if (!$this->cursoscurso->contains($cursoscurso)) {
$this->cursoscurso[] = $cursoscurso;
}

return $this;
}

public function removeCursoscurso(Cursos $cursoscurso): self
{
if ($this->cursoscurso->contains($cursoscurso)) {
$this->cursoscurso->removeElement($cursoscurso);
}

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function __toString(){
return $this->name;
}
}

El resultado final es que no muestra nada y ya no se que hacer ¿Me podrian ayudar?

Muchas Gracias

2
  • El rol lo guardas en la tabla usuario en un tabla intermedia de rol_usuario? el 11 abr. 2019 a las 16:22
  • En la misma tabla usuario (Perdona por la tardanza)
    – Jaime
    el 16 abr. 2019 a las 19:35

1 respuesta 1

0

Ya lo solucioné

Lo realicé con un simple LIKE

 return $this->getEntityManager()
    ->createQuery(
        "SELECT u FROM App\Entity\User u WHERE u.roles LIKE '%ROLE_PROFE%'"
    )
    ->getResult();

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.