使用代码:
$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
答案 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();