symfony2: how to use group_concat in QueryBuilder

后端 未结 4 687
眼角桃花
眼角桃花 2020-12-01 09:11

I am having nested-set (using Gedmo tree) entity called \"Location\". Entity \"Appartment\" has location_id and what I need to do it to map scalar value called eg \"path\" t

4条回答
  •  误落风尘
    2020-12-01 10:06

    If any comes across this post, there's now a Doctrine Extensions repository in Github. The repo has instructions on how to use it. You can use composer to install it and then use any function you're interested in.

    EDIT:

    For the Sake of the user Tushar, the way to use GROUP_CONCAT in Doctrine2's DQL is simple install Doctrine Extensions:

    composer require beberlei/DoctrineExtensions
    

    To enable it: add the following in your config.yml:

    doctrine:
     orm:
         dql:
             string_functions:
                 group_concat: DoctrineExtensions\Query\Mysql\GroupConcat
    

    Then in your code, you should be able now to use Group Concat in your DQLs:

    $this->createQueryBuilder('location')
                ->select('location')
                ->addSelect("GROUP_CONCAT(DISTINCT location.name SEPARATOR '; ') AS locationNames");
    
            $result = $queryBuilder->getQuery()->getResult();
    

    Or in the case for the original question:

    $query->addSelect("(SELECT GROUP_CONCAT(k99.name ORDER BY k99.level SEPARATOR '$separator') FROM Location k99 WHERE $subquery) AS path");
    

提交回复
热议问题