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

当我执行$qb->getQuery()->getResult()时,我收到此错误:

致命错误:在 /var/www/darkfrog/application/libraries/Doctrine/ORM 中未捕获的异常 'Doctrine\ORM\Query\QueryException' 和消息 'SELECT count(u.name) FROM User u WHERE u.name = :name' /Query/QueryException.php:39堆栈跟踪:

#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):/var/www/darkfrog/application/libraries/Doctrine/ORM/Query/QueryException 中的 Doctrine\ORM\Query\Pars .php 在第 49 行

4

3 回答 3

1

以下代码效果很好,只需在子句中添加“实体\”。

$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()); 
于 2013-08-12T19:11:10.100 回答
0
$qb->$this->doctrine->em->createQueryBuilder()
    ->select($qb->expr()->count('u.name'))
    ->from('User','u')
    ->where('u.name = :name')
    ->setParameter('name', $user->getUsername());
于 2013-08-11T10:32:30.177 回答
0

您可以使用 User::class 来定义实体名称。

$qb = $this->doctrine->em->createQueryBuilder()                
            ->from(User::class,'u')
            ->select('count(u.name)')
            ->where('u.name = :name')
            ->setParameter('name', $user->getUsername());
于 2019-01-18T13:10:56.243 回答