Created
June 11, 2014 12:10
-
-
Save tentacode/3c62aa3db5aa016abcc6 to your computer and use it in GitHub Desktop.
Adding MySQL GROUP_CONCAT to doctrine in a Symfony project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/config.yml | |
# ... | |
doctrine: | |
orm: | |
entity_managers: | |
default: | |
dql: | |
string_functions: | |
GROUP_CONCAT: Tentacode\LolCatBundle\Doctrine\Mysql\GroupConcat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can make a PR to https://github.com/luxifer/doctrine-functions ;)