Skip to content

Check Doctrine query parameters used in incompatibile comparisons.

License

Notifications You must be signed in to change notification settings

shipmonk-rnd/doctrine-query-checker

Repository files navigation

Doctrine Query Checker

Doctrine Query Tree Walker that perform additional checks on the query AST in addition to the default checks performed by Doctrine.

Currently it checks that the types of the parameters passed to the query are correct. For example the following will result in exception:

// throws: Parameter 'created_at' has no type specified in 3rd argument of setParameter(). Thus it is inferred as 'string', but it is compared with 'u.createdAt' which can only be compared with 'datetime_immutable'.
$this->entityManager->createQueryBuilder()
    ->select('u')
    ->from(User::class, 'u')
    ->where('u.createdAt < :created_at')
    ->setParameter('created_at', 'not a date')
    ->getQuery()
    ->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [QueryCheckerTreeWalker::class]);
    ->getResult();

If you want to log the exceptions instead of throwing them, you can pass a logger to the QueryCheckerTreeWalker:

QueryCheckerTreeWalker::setLogger($logger);

Installation

composer require shipmonk/doctrine-query-checker

Enabling for a specific query

use Doctrine\ORM\Query;
use ShipMonk\DoctrineQueryChecker\QueryCheckerTreeWalker;

$query = $this->entityManager->createQueryBuilder()
    ->select('u')
    ->from(User::class, 'u')
    ->getQuery()
    ->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [QueryCheckerTreeWalker::class]);

Enabling for all queries

use Doctrine\ORM\Query;
use ShipMonk\DoctrineQueryChecker\QueryCheckerTreeWalker;

$this->entityManager->getConfiguration()
    ->setDefaultQueryHint(Query::HINT_CUSTOM_TREE_WALKERS, [QueryCheckerTreeWalker::class]);

About

Check Doctrine query parameters used in incompatibile comparisons.

Resources

License

Stars

Watchers

Forks

Languages