Query analyser gets confused when a join alias is equal to the relation name

Open
#554 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
devtools

Research direction

Start by reproducing the reported QueryBuilder sequence, especially the leftJoin where the alias equals the relation name, and call getQuery() to trigger analysis. Compare it with the version using the alias "u"; done means the equivalent alias should no longer produce the dynamic-arguments error.

Written by the indexing model from the issue text.

Description

Hi,

this code fragment triggers a “Could not analyse QueryBuilder with dynamic arguments” error.

$builder = $entityManager->createQueryBuilder();
$builder->from(MyEntity::class, 'rr');
$builder->addSelect('rr');
$builder->leftJoin('rr.user', 'user');
$builder->addSelect('user');
$builder->leftJoin('user.role', 'r');
$builder->addSelect('r');

// Error here: Could not analyse QueryBuilder with dynamic arguments.
$query = $builder->getQuery();

But as soon as I change the alias to be different from the relation name, the analyser stops raising the error:

$builder = $entityManager->createQueryBuilder();
$builder->from(MyEntity::class, 'rr');
$builder->addSelect('rr');
$builder->leftJoin('rr.user', 'u');
$builder->addSelect('u');
$builder->leftJoin('u.role', 'r');
$builder->addSelect('r');

// no error
$query = $builder->getQuery();

I have no problem changing the alias name, but I had no idea that this error could be triggered by that . I had to resort to putting $builder->getQuery(); after every line to see which line set it off :)

I’m using the latest versions of phpstan with 2.x versions of doctrine packages:

phpstan/extension-installer 1.3.1
phpstan/phpdoc-parser       1.27.0
phpstan/phpstan             1.10.64
phpstan/phpstan-doctrine    1.3.64
phpstan/phpstan-symfony     1.3.9
doctrine/dbal               2.13.9
doctrine/orm                2.11.3
Dominant language
PHP
Stars
678
Forks
122
Avg merge
5d 18h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-doctrine

All issues in phpstan/phpstan-doctrine

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.