0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Symfony4 Doctrineでconvert関数を使う

Last updated at Posted at 2020-09-19

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%' );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?