Search code examples
doctrine-orm

"Semantical Error" line 0, col 14 near 'User u WHERE': Error: Class 'User' is not defined


I have a Symfony 5 project which has default authentication and User entity. I was reading Doctrine documentation in order to use $em->createQueryBuilder() function but I got this error:

[Semantical Error] line 0, col 14 near 'User u WHERE': Error: Class 'User' is not defined.

I just need to query some users with an specific role so I wrote the following code in the controller:

$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$category_users = $qb->select(array('u'))
    ->from('User', 'u')
    ->where('u.category = ' . $category->getId())
    ->setParameter('roles', '%"ROLE_CUSTOM"%')
    ->orderBy('u.name', 'ASC')
    ->getQuery()
    ->getResult();

I know the error does not have to do with writing the query itself and I guess this could have two questions, but just in case...

Question 1: What am I forgetting to get this error? I have read in previous versions of Symfony I should add Bundle name to from('User', 'u') but I am not sure about this version.

Question 2: Is this code optimal for this kind of query? Or maybe I am overcomplicating stuff?


Solution

  • You must use a FqCN for the Entity.

    e.g. from('App\Entity\User', 'u')