Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantical Error on conditionally added joins to the QueryBuilder #624

Open
yguedidi opened this issue Nov 8, 2024 · 1 comment
Open

Comments

@yguedidi
Copy link

yguedidi commented Nov 8, 2024

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();
@ServerExe
Copy link

ServerExe commented Jan 27, 2025

Same here. When using private methods to join tables but using the joined aliases in the select([]) method, PHPStan complains.

Example:

$qb = $this
    ->createQueryBuilder('foo')
    ->select(['foo', 'bar', 'baz']);

$this
    ->joinBar($qb)
    ->joinBaz($qb);

It complains:

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   src/Repository/FooRepository.php                                                                                  
 ------ ------------------------------------------------------------------------------------------------------------------- 
  64     QueryBuilder: [Semantical Error] line 0, col 13 near 'bar, baz,': Error: 'bar' is not defined.  
         🪪  doctrine.dql                                                                                                                                                                                                 
 ------ -------------------------------------------------------------------------------------------------------------------

These are my methods:

private function joinBar(QueryBuilder $qb): self
{
    $qb->join(\sprintf('%s.bar', 'foo'), 'bar');

    return $this;
}

private function joinBaz(QueryBuilder $qb): self
{
    $qb->join(\sprintf('%s.baz', 'foo'), 'baz');

    return $this;
}

Side note: Reason why I am using sprintf() is because actually I am using constants but for presentation reasons here I used pure strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants