Symfony2:调用未定义的方法Doctrine \ ORM \ QueryBuilder :: getResult()

时间:2014-01-19 21:33:30

标签: php symfony doctrine-orm doctrine

使用代码:

$qb = $entityManager->createQueryBuilder();
$qb->select('cat')->from('BuyAndSellSiteBundle:Category ', 'cat');
$qb->getQuery();
$a =$qb->getResult();

我得到一个例外:

FatalErrorException: Error: Call to undefined method Doctrine\ORM\QueryBuilder::getResult() in C:\xampp\htdocs\buySell\src\BuyAndSell\SiteBundle\Controller\DefaultController.php line

2 个答案:

答案 0 :(得分:19)

这是有道理的。由于QueryBuilder不会自行更改,因此您需要存储getQuery()调用的结果:

$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('cat')->from('BuyAndSellSiteBundle:Category ', 'cat');

// get the Query from the QueryBuilder here ...
$query = $qb->getQuery();

// ... then call getResult() on the Query (not on the QueryBuilder)
$result = $query->getResult(); 

答案 1 :(得分:4)

更改为:

$a = $qb->getQuery()->getResult();

// or

$q = $qb->getQuery();
$a = $q->getResult();