我创建了名为DATESTRING的教义扩展,但我得到了错误。
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%M ))"
月字符串函数.php
use DoctrineORMQueryASTFunctionsFunctionNode;
use DoctrineORMQueryLexer;
use DoctrineORMQuerySqlWalker;
use DoctrineORMQueryParser;
/**
* "MONTHSTRING" "(" SimpleArithmeticExpression ")"
*
* @category DoctrineExtensions
*/
class MonthStringFunction extends FunctionNode
{
public $date;
/**
* @override
*/
public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
{
return "MONTHSTRING(DATE_FORMAT(" . $sqlWalker->walkArithmeticPrimary($this->date) . ", %M ))"; //**here some error**
}
/**
* @override
*/
public function parse(DoctrineORMQueryParser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->date = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
测试存储库.php
$qb = $this->createQueryBuilder('tst');
$qb->select('MONTHSTRING(tst.joinDate) as month_string');
$data = $qb->getQuery()->getArrayResult();
return $data;
DATE_FORMAT
函数的格式参数需要用引号括起来,因为它是一个字符串。试试这个:
public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
{
return "MONTHSTRING(DATE_FORMAT(" . $sqlWalker->walkArithmeticPrimary($this->date) . ", '%M' ))"; //**here some error**
}