Skip to content

Instantly share code, notes, and snippets.

@tentacode
Created June 11, 2014 12:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tentacode/3c62aa3db5aa016abcc6 to your computer and use it in GitHub Desktop.
Save tentacode/3c62aa3db5aa016abcc6 to your computer and use it in GitHub Desktop.
Adding MySQL GROUP_CONCAT to doctrine in a Symfony project
# app/config.yml
# ...
doctrine:
orm:
entity_managers:
default:
dql:
string_functions:
GROUP_CONCAT: Tentacode\LolCatBundle\Doctrine\Mysql\GroupConcat
<?php
// src/Tentacode/LolcatBundle/Doctrine/Mysql/GroupConcat.php
namespace Tentacode\LolcatBundle\Doctrine\Mysql;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Parser;
class GroupConcat extends FunctionNode
{
protected $isDistinct = false;
protected $expression = null;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf('GROUP_CONCAT(%s%s)',
$this->isDistinct ? 'DISTINCT ' : '',
$this->expression->dispatch($sqlWalker)
);
}
public function parse(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);
}
}
@luxifer
Copy link

luxifer commented Jun 18, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment