5

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?

2 Answers 2

11

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'
            );
    }
}
1

You can't globally set this hint. So to solve your problem i see two ways:

1) register and use your own doctrine function for ILIKE (example here https://github.com/domudall/DoctrineExtensions/blob/master/lib/Dmno/DoctrineExtensions/Comparison/Postgresql/Ilike.php)

2) Use AOP interceptor (https://github.com/schmittjoh/JMSAopBundle/blob/master/Resources/doc/index.rst) to insert $query->setHint.. before every execute of a query.

1
  • To bad there is no solution. I'll accept this because while it does not solve the problem it provides path forward. I'll checkout the xtension later.
    – Lighthart
    Commented Apr 28, 2013 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.