Skip to content

Instantly share code, notes, and snippets.

@77web
Created March 9, 2015 04:41
Show Gist options
  • Save 77web/43da0a19414f22d9614b to your computer and use it in GitHub Desktop.
Save 77web/43da0a19414f22d9614b to your computer and use it in GitHub Desktop.
メモ:Doctrine2でmysqlのYEAR,Monthを使えるようにするためのExtension
<?php
namespace Nanaweb\Doctrine\DateFunctionExtension;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* MonthFunction ::=
* "MONTH" "(" ArithmeticPrimary ")"
*/
class Month extends FunctionNode
{
public $dateExpression = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->dateExpression = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return 'MONTH('.$this->dateExpression->dispatch($sqlWalker).')';
}
}
<?php
namespace Nanaweb\Doctrine\DateFunctionExtension;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* MonthFunction ::=
* "YEAR" "(" ArithmeticPrimary ")"
*/
class Year extends FunctionNode
{
public $dateExpression = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->dateExpression = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return 'YEAR('.$this->dateExpression->dispatch($sqlWalker).')';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment