How to return only one column

Asked

Viewed 120 times

0

People I have the following query made.

$ent->createQueryBuilder('t')
                ->select('count(t.id)')
                ->join('t.categoria', 'c')
                ->join('t.noticia', 'n')
                ->groupBy('n.id')
                ->where('n.posicao=1')
                ->andWhere('c.id=16')
                ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
                ->getQuery()
                ->getSingleScalarResult();

Only that it is not returning me only a column with Count. I believe it is because of groupBy. How I hit it to only return Count’s total without taking out groupBy. Thank you

  • What the query is returning?

  • @Rodrigorigotti Returns an error saying that multiple columns are being returned.

  • @Rodrigorigotti this is the error message returned: The query returned multiple rows. Change the query or use a different result function like getScalarResult()

2 answers

0

The query generated by QueryBuilder is wrong, since it is returning not only the result of the COUNT(t.id), but also all columns of t.

Exchange the above code for:

$ent->createQueryBuilder()
     ->select('count(t.id)')
     ->join('t.categoria', 'c')
     ->join('t.noticia', 'n')
     ->groupBy('n.id')
     ->where('n.posicao=1')
     ->andWhere('c.id=16')
     ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
     ->getQuery()
     ->getSingleScalarResult();

0


Guys I managed to solve using Distinct. It was like this:

$ent->createQueryBuilder('t')
            ->select('count(DISTINCT c.id)')
            ->join('t.categoria', 'c')
            ->join('t.noticia', 'n')
            ->where('n.posicao=1')
            ->andWhere('c.id=16')
            ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
            ->getQuery()
            ->getSingleScalarResult();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.