Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Call to undefined method Doctrine\ORM\QueryBuilder::getResult()

With the code:

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

I get an exception:

FatalErrorException: Error: Call to undefined method Doctrine\ORM\QueryBuilder::getResult() in C:\xampp\htdocs\buySell\src\BuyAndSell\SiteBundle\Controller\DefaultController.php line
like image 833
LanMan Avatar asked Jan 19 '14 21:01

LanMan


2 Answers

This makes sense. Because QueryBuilder does not change itself, you need to store result of getQuery() invocation :

$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(); 
like image 79
Jovan Perovic Avatar answered Nov 04 '22 03:11

Jovan Perovic


Change to:

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

// or

$q = $qb->getQuery();
$a = $q->getResult();
like image 4
voodoo417 Avatar answered Nov 04 '22 02:11

voodoo417