2
votes
$qb = $this->doctrine->em->createQueryBuilder()                
            ->from('User','u')
            ->select('count(u.name)')
            ->where('u.name = :name')
            ->setParameter('name', $user->getUsername());

When I execute $qb->getQuery()->getResult(), I get this error:

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT count(u.name) FROM User u WHERE u.name = :name' in /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/QueryException.php:39Stack trace:

#0 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(429): Doctrine\ORM\Query\QueryException::dqlError('SELECT count(u....')

#1 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(854): Doctrine\ORM\Query\Parser->semanticalError('Class 'User' is...', Array)

#2 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(1529): Doctrine\ORM\Query\Parser->AbstractSchemaName()

#3 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(1426): Doctrine\ORM\Query\Parser->RangeVariableDeclaration()

#4 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(1168): Doctrine\ORM\Query\Parser->IdentificationVariableDeclaration()

#5 /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/Parser.php(757): Doctrine\ORM\Query\Pars in /var/www/darkfrog/application/libraries/Doctrine/ORM/Query/QueryException.php on line 49

3

3 Answers

1
votes

the following code works great, just add "Entities\" in the clause from.

$qb = $this->doctrine->em->createQueryBuilder()
            ->select($this->doctrine->em->createQueryBuilder()->expr()->count('u.username'))
            ->from('Entities\User','u')
            ->where('u.username = :username')
            ->setParameter('username', $user->getUsername());
var_dump($qb->getQuery()->getResult()); 
0
votes
$qb->$this->doctrine->em->createQueryBuilder()
    ->select($qb->expr()->count('u.name'))
    ->from('User','u')
    ->where('u.name = :name')
    ->setParameter('name', $user->getUsername());
0
votes

You can use a User::class to define the entity name.

$qb = $this->doctrine->em->createQueryBuilder()                
            ->from(User::class,'u')
            ->select('count(u.name)')
            ->where('u.name = :name')
            ->setParameter('name', $user->getUsername());