Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply custom doctrine output walker by default to all queries

I have written the following custom walker to use postgres's ilike instead of like:

use Doctrine\ORM\Query\SqlWalker;

class IlikeWalker extends SqlWalker
{
     /**
     * Walks down a SelectClause AST node, thereby generating the desired SQL.
     *
     * @param $selectClause
     * @return string The SQL.
     */
    public function walkLikeExpression($likeExpr)
    {
        $sql = parent::walkLikeExpression($likeExpr);
        $sql = str_replace('LIKE', 'ILIKE', $sql);
        return $sql;
    }
}

which may be added to any query via:

      $query->setHint( $query::HINT_CUSTOM_OUTPUT_WALKER
                     ,'\DoctrineExtensions\WalkerBundle\Walker\IlikeWalker' );

But how do I start a service or apply a config to automatically use this for every query?

like image 364
Lighthart Avatar asked Apr 27 '13 02:04

Lighthart


1 Answers

Doctrine 2.5 has a new option defaultQueryHint on orm configuration. You can setup a custom walker once for all queries:

<?php
/** @var \Doctrine\ORM\EntityManager $em */
$em->getConfiguration()->setDefaultQueryHint(
    Query::HINT_CUSTOM_OUTPUT_WALKER,
    '\DoctrineExtensions\WalkerBundle\Walker\IlikeWalker'
)

You can use this code in boot method of your bundle class:

class YouBundle extends Bundle
{
    public function boot()
    {
        parent::boot();
        $this->container
            ->get('doctrine.orm.entity_manager')
            ->getConfiguration()
            ->setDefaultQueryHint(
                Query::HINT_CUSTOM_OUTPUT_WALKER,
                '\DoctrineExtensions\WalkerBundle\Walker\IlikeWalker'
            );
    }
}
like image 175
Artur Eshenbrener Avatar answered Nov 15 '22 07:11

Artur Eshenbrener