Y
Y
Yuri Petrashevich2016-08-13 23:30:28
MySQL
Yuri Petrashevich, 2016-08-13 23:30:28

How to set condition using innerJoin in createQueryBuilder?

It is necessary that if $statuses is present, innerJoin is used, and if $statuses is absent, it is not used.

$all = $this
                ->createQueryBuilder('u')
                ->select('count(u.id)')
                ->innerJoin('u.roles', 'r', 'WITH', 'r.id IN (' . $statuses . ')')
                ->getQuery()
                ->getSingleScalarResult();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Skobkin, 2016-08-14
@skobkin

What's the problem?

// За форматирование извиняюсь - писал всё вручную
$qb = $this->createQueryBuilder('u')->select('...');
if ($statuses) {
    $qb->innerJoin()
}
$all = $qb->getQuery()->getSingleScalarResult();

Learn PHP or something.

Y
Yuri Petrashevich, 2016-08-14
@BelCoder

This is how it works

$qb = $this->createQueryBuilder('u')->select('...');
if ($statuses) {
    $qb = $qb->innerJoin()
}
$qb = $qb->getQuery();
$all = $qb->getSingleScalarResult();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question