Created
June 20, 2016 12:26
-
-
Save miedzwin/97bc59a2e938df6a53250a5a315bfd83 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace AppBundle\DoctrineExtensions\Postgres; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\Lexer; | |
/** | |
* | |
*/ | |
class ArrayAgg extends FunctionNode | |
{ | |
public $field; | |
/** | |
* @override | |
*/ | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | |
{ | |
return "array_agg(" . $sqlWalker->walkArithmeticPrimary($this->field) . ")"; | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->field = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
This file contains 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 | |
namespace AppBundle\DoctrineExtensions\Postgres; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\Lexer; | |
/** | |
* | |
*/ | |
class ArrayToJson extends FunctionNode | |
{ | |
public $field; | |
/** | |
* @override | |
*/ | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | |
{ | |
return "array_to_json(" . $sqlWalker->walkArithmeticPrimary($this->field) . ")"; | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->field = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
This file contains 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
orm: | |
auto_generate_proxy_classes: "%kernel.debug%" | |
naming_strategy: doctrine.orm.naming_strategy.underscore | |
auto_mapping: true | |
dql: | |
string_functions: | |
ARRAYAGG: AppBundle\DoctrineExtensions\Postgres\ArrayAgg | |
ARRAYTOJSON: AppBundle\DoctrineExtensions\Postgres\ArrayToJson |
This file contains 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 | |
namespace AppBundle\Repository; | |
use Doctrine\ORM\EntityRepository; | |
/** | |
* UserRepository | |
* | |
* This class was generated by the Doctrine ORM. Add your own custom | |
* repository methods below. | |
*/ | |
class UserRepository extends EntityRepository | |
{ | |
/** | |
* Method return all users for admin | |
* @return array | |
*/ | |
public function getArrayForAdminList() | |
{ | |
$qb = $this->createQueryBuilder('u') | |
->select( | |
'u.id AS HIDDEN id, u.roles, u.firstName, u.lastName, u.email, u.created, u.updated, u.isNewsletter, ARRAYTOJSON(ARRAYAGG(s.title)) AS user_school_title, ARRAYTOJSON(ARRAYAGG(sbj.title)) AS user_subject_title' | |
) | |
->leftJoin('u.userSchools', 'usql') | |
->leftJoin('usql.school', 's') | |
->leftJoin('usql.userSchoolSubjects', 'usbj') | |
->leftJoin('usbj.subject', 'sbj') | |
->addGroupBy('u.id'); | |
$aResult = $qb->getQuery()->getArrayResult(); | |
$newResult = []; | |
foreach ($aResult as $result) { | |
$aTmp = []; | |
foreach ($result as $key => $value) { | |
if ($this->isJSON($value)) { | |
$aTmp[$key] = implode(',', json_decode($value)); | |
} else { | |
$aTmp[$key] = $value; | |
} | |
} | |
$newResult[] = $aTmp; | |
} | |
return $newResult; | |
} | |
private function isJSON($string) | |
{ | |
return is_string($string) && is_array(json_decode($string, true)) && (json_last_error() == JSON_ERROR_NONE) ? true : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment