phpstan / phpstan-doctrine

Doctrine extensions for PHPStan
MIT License
598 stars 97 forks source link

Semantical Error on conditionally added joins to the QueryBuilder #624

Open yguedidi opened 2 weeks ago

yguedidi commented 2 weeks ago

I got this error

QueryBuilder: [Semantical Error] line X, col X near 't.other IN(':
Error: 't' is not defined.

With the following code:

        if (empty($things) && empty($others)) {
            return [];
        }

        $qb = $this->createQueryBuilder('s');

        $condition = $qb->expr()->orX();

        if (!empty($things)) {
            $condition->add('s.thing IN(:things)');
            $qb->setParameter('things', $things);
        }

        if (!empty($others)) {
            $condition->add('t.other IN(:others)');
            $qb
                ->addSelect('t')
                ->join('s.thing', 't')
                ->setParameter('others', $others)
            ;
        }

        $qb->andWhere($condition);

        return $qb->getQuery()->getResult();