Search code examples
phpdoctrine-ormdoctrinedql

Error: Expected Literal, got '*' in Doctrine


I want to retrieve the number of rows in my courrier table.

In my fixtures, I have these lines :

$query = $this->em->createQuery('SELECT count(*) FROM App\Entity\Courrier');
$countCourrier = $query->getResult();

I have these mistakes when I do symfony console doctrine:fixtures:load :

In QueryException.php line 38:
                                                               
[Syntax Error] line 0, col 13: Error: Expected Literal, got '*'  
                                                               

In QueryException.php line 27:
                                        
SELECT count(*) FROM App\Entity\Courrier  
                                       

What's wrong with the * in my query ?


Solution

  • Doctrine expects you to use one of the fields of your entity. In SQL you don't need to use count(*) to get the amount of rows count(id) also works as long as the column is not nullable.

    So changing your query to SELECT count(c.id) FROM App\Entity\Courrier as c should solve this.