DQLユーザー定義クラスを適当なServive内とかに作る
<?php
namespace App\Service\Doctrine;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Convert extends FunctionNode
{
protected $dateExpression;
protected $fromTz;
protected $toTz;
/**
* {@inheritdoc}
*/
public function getSql(SqlWalker $sqlWalker)
{
return sprintf("CONVERT(%s USING %s)",
$sqlWalker->walkArithmeticPrimary($this->field),
//$sqlWalker->walkSimpleArithmeticExpression($this->using), // or remove USING and uncomment this
$sqlWalker->walkSimpleArithmeticExpression($this->charset)
);
}
/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->field = $parser->ArithmeticPrimary();
// adopt use bypass validate variable of parse by using AliasResultVariable ...!!
$this->using = $parser->AliasResultVariable();
$this->charset = $parser->AliasResultVariable();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
参考:https://github.com/beberlei/DoctrineExtensions/tree/master/src/Query/Mysql
定義した関数を登録
config\packages\doctrine.yaml
doctrine:
dql:
string_functions:
convert: App\Service\Doctrine\Convert
使用する
$qb->andwhere('convert(i.etcField USING utf8) like :etcField')->setParameter('etcField','%TEST%' );