Sha*_*awn 8 mysql doctrine-orm
我从以下文档中了解到:http: //docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#dql-functions 没有ROUND函数但有一个简单的方法没有编写我自己的DQL类函数就可以做到这一点?
编辑: 如果做平均值并且返回整数是可能的,我不需要完全匹配.
您需要为此实现自定义DQL函数.
DoctrineExtensions中有一些例子.
您可以像下面这样实现它:
<?php
namespace MyApp\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\SqlWalker;
class Round extends FunctionNode
{
private $arithmeticExpression;
public function getSql(SqlWalker $sqlWalker)
{
return 'ROUND(' . $sqlWalker->walkSimpleArithmeticExpression(
$this->arithmeticExpression
) . ')';
}
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->arithmeticExpression = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在引导ORM时在配置中注册它:
$config = new \Doctrine\ORM\Configuration();
$config->addCustomNumericFunction('ROUND', 'MyApp\DQL\Round');
Run Code Online (Sandbox Code Playgroud)
更清洁的方法是使用略微修改的@Ocramius代码.
将这段代码放在:src/YourNamespace/YourMainBundle/DoctrineFunctions/directory作为Round.php文件名:
<?php
namespace YourApp\YourMainBundle\DoctrineFunctions;
use Doctrine\ORM\Query\AST\Functions\FunctionNode,
Doctrine\ORM\Query\Lexer;
class Round extends FunctionNode
{
private $arithmeticExpression;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->arithmeticExpression = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'ROUND(' . $sqlWalker->walkSimpleArithmeticExpression($this->arithmeticExpression) . ')';
}
}
Run Code Online (Sandbox Code Playgroud)
然后把它放在你的app/config/config.yml:
doctrine:
dql:
numeric_functions:
round: YourApp\YourMainBundle\DoctrineFunctions\Round
Run Code Online (Sandbox Code Playgroud)
这将允许您ROUND()直接在DQL SELECT查询中使用该函数; 无论是使用QueryBuilder还是直接通过createQuery()
如果您希望能够指定舍入精度,可以使用此处提供的类。如果您使用的是 symfony,请安装该包,因为您还将获得额外的标准 mysql 功能。
链接资源的代码也可以在下面找到:
<?php
namespace Mapado\MysqlDoctrineFunctions\DQL;
use \Doctrine\ORM\Query\AST\Functions\FunctionNode;
use \Doctrine\ORM\Query\Lexer;
/**
* MysqlRound
*
* @uses FunctionNode
* @author Julien DENIAU <julien.deniau@mapado.com>
*/
class MysqlRound extends FunctionNode
{
/**
* simpleArithmeticExpression
*
* @var mixed
* @access public
*/
public $simpleArithmeticExpression;
/**
* roundPrecision
*
* @var mixed
* @access public
*/
public $roundPrecision;
/**
* getSql
*
* @param \Doctrine\ORM\Query\SqlWalker $sqlWalker
* @access public
* @return string
*/
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'ROUND(' .
$sqlWalker->walkSimpleArithmeticExpression($this->simpleArithmeticExpression) .','.
$sqlWalker->walkStringPrimary($this->roundPrecision) .
')';
}
/**
* parse
*
* @param \Doctrine\ORM\Query\Parser $parser
* @access public
* @return void
*/
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->roundPrecision = $parser->ArithmeticExpression();
if ($this->roundPrecision == null) {
$this->roundPrecision = 0;
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6834 次 |
| 最近记录: |