问题描述
我尝试使用GROUP_CONCAT()在doctrine使用querybuilder()
我尝试这个
$ qb = $ this-> em-> createQueryBuilder();
$ qb-> select('category.id industry_id,category.name industry_name,group_concat(category.name)')
,但它不能以这种方式工作。
我还提到
但是提供了所有链接由#a.aitboudad正在进行404.任何人都可以给我链接或做某事吗?
谢谢
您将需要创建自己的Doctrine函数来使用GROUP_CONCAT;
config.yml;
orm:
dql:
string_functions:
GROUP_CONCAT:Your\Bundle\DQL\GroupConcat
Your\Bundle\DQL\GroupConcat.php;
(来源:)
使用Doctrine\ORM\Query\Lexer;
使用Doctrine\ORM\Query\AST\Functions\FunctionNode;
class GroupConcat extends FunctionNode
{
public $ isDistinct = false;
public $ expression = null;
public function getSql(\Doctrine\ORM\Query\SqlWalker $ sqlWalker)
{
return'GROUP_CONCAT('。
($ this- isDistinct?'DISTINCT':'')
$ this-> expression-> dispatch($ sqlWalker)
')';
}
public function parse(\Doctrine\ORM\Query\Parser $ parser)
{
$ parser->匹配(词法分析器:: T_IDENTIFIER);
$ parser-> match(Lexer :: T_OPEN_PARENTHESIS);
$ lexer = $ parser-> getLexer();
if($ lexer-> isNextToken(Lexer :: T_DISTINCT)){
$ parser-> match(Lexer :: T_DISTINCT);
$ this-> isDistinct = true;
}
$ this-> expression = $ parser-> SingleValuedPathExpression();
$ parser-> match(Lexer :: T_CLOSE_PARENTHESIS);
}
}
然后在查询构建器(或DQL) / p>
$ qb-> select('GROUP_CONCAT(category.name)');
I am trying to use GROUP_CONCAT() in doctrine using querybuilder()
I tried this
$qb=$this->em->createQueryBuilder();
$qb->select('category.id industry_id,category.name industry_name,group_concat(category.name)')
but its not working this way.
I also refered symfony2: how to use group_concat in QueryBuilder
but all the link provided by #a.aitboudad are going 404. can anyone give me link or something to do it ?
thanks
You will need to create your own Doctrine function to use GROUP_CONCAT;
config.yml;
orm:
dql:
string_functions:
GROUP_CONCAT: Your\Bundle\DQL\GroupConcat
Your\Bundle\DQL\GroupConcat.php;
(source: https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/GroupConcat.php)
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
class GroupConcat extends FunctionNode
{
public $isDistinct = false;
public $expression = null;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'GROUP_CONCAT(' .
($this->isDistinct ? 'DISTINCT ' : '') .
$this->expression->dispatch($sqlWalker) .
')';
}
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$lexer = $parser->getLexer();
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
$parser->match(Lexer::T_DISTINCT);
$this->isDistinct = true;
}
$this->expression = $parser->SingleValuedPathExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Then in your query builder (or DQL);
$qb->select('GROUP_CONCAT(category.name)');
这篇关于symfony2:在QueryBuilder(Doctrine)中使用group_concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!