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?
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'
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With