Skip to content

Semantical Error on conditionally added joins to the QueryBuilder #624

Open
@yguedidi

Description

@yguedidi

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions