N
N
nepster-web2017-12-04 13:29:17
Doctrine ORM
nepster-web, 2017-12-04 13:29:17

Doctrine, how to count the number of records with join?

There is a request to get data (request shortened for convenience):

$qb = $this->entityManager->createQueryBuilder()
            ->select(['record', 'type'])
            ->from(Record::class, 'record')
            ->leftJoin('record.type', 'type');

        $criteria = Criteria::create();
        $criteria->andWhere(Criteria::expr()->eq('type.alias', 'news'));
        $qb->addCriteria($criteria);

        $query = $qb->getQuery();

        $query->getScalarResult();

I need to do the same thing, only get the number of records as a scalar number.
It is clear that the option with count($query->getScalarResult()) is not suitable for optimization.
Tell me, how can I count the number of records, given the join?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-12-04
@BorisKorobkov

Option 1. Run a separate query SELECT COUNT(*) ..., i.e. $qb->count()
Option 2 (a crutch for MySQL only). https://dev.mysql.com/doc/refman/5.7/en/informatio...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question