Doctrine QueryBuilder:未捕获的异常

时间:2013-08-11 09:36:12

标签: php codeigniter doctrine

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

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

  

致命错误:未捕获异常'Doctrine \ ORM \ Query \ QueryException',并在/ var / www / darkfrog / application / libraries /中显示消息'SELECT count(u.name)FROM User u WHERE u.name =:name' 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 ......',数组)

     

#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 \ Pars第49行的/Doctrine/ORM/Query/QueryException.php

3 个答案:

答案 0 :(得分: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()); 

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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