website/errors/doctrine.queryBuilderDynamicArgument.md
<?php declare(strict_types = 1);
use Doctrine\ORM\EntityManagerInterface;
function buildQuery(EntityManagerInterface $em, string $field): void
{
$qb = $em->createQueryBuilder()
->select('u')
->from('App\Entity\User', 'u')
->where("u.$field = :value")
->setParameter('value', 'test');
$qb->getQuery();
}
This error is reported by the phpstan-doctrine extension.
PHPStan could not statically analyse the QueryBuilder because it contains dynamic (non-constant) arguments. In the example above, the $field variable makes the DQL string dynamic, preventing PHPStan from validating the query at analysis time.
Use constant string values in QueryBuilder method calls so that PHPStan can statically determine the resulting DQL:
<?php declare(strict_types = 1);
use Doctrine\ORM\EntityManagerInterface;
-function buildQuery(EntityManagerInterface $em, string $field): void
+function buildQuery(EntityManagerInterface $em): void
{
$qb = $em->createQueryBuilder()
->select('u')
->from('App\Entity\User', 'u')
- ->where("u.$field = :value")
+ ->where('u.email = :value')
->setParameter('value', 'test');
$qb->getQuery();
}