I want get results of my query (with limit 10) + count possible results. I know there is similar questions and answers.
but if i trying get count possible rows (via getSingleScalarResult()
) i will get excepton: The query returned multiple rows. Change the query or use a different result function like getScalarResult().
$query = $repository
->createQueryBuilder('t')
->select('COUNT(t.katId)', 't.hotel', 't.title', 't.desc', 'picture', 'MIN(t.price) AS price');
$query->where('t.visible = (:visible)')->setParameter('visible', 1);
// + some wheres, where in, more than....
$query->groupBy('t.hotel');
$query->setMaxResults(10);
echo $query->getQuery()->getSingleScalarResult();
exit();
I just need one integer whitch represent all results from my query. How can i get this count number? Ideal in one shot to db.
EDIT:
if i remove $query->groupBy('t.hotel');
and in select keep only ->select('count(t.katId)');
then it work. But i need groupBy
because it makes real count of results.
SOLUTION
I divided it on two queries so - to get results i rolled back changes to state before trying any count information, and make clone this query (before set setMaxResults
and groupBy
), change select (keep all wheres) and get count information.
I will be grateful if someone offers better solution
Get results:
- removed COUNT() from select
- asking for results changed to 'normal'
->getArrayResults
Get count:
$q = clone $query; $q->select('count(distinct t.hotel) as count'); $r = $q->getQuery()->getArrayResult(); echo $r[0]['count']; exit();