Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

symfony - symfony2: how to use group_concat in QueryBuilder

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" to query that returns all appartments.

In Doctrine1, I had this code:

/**
* Add "path" to each element
* 
* @param Doctrine_Query $query
* @param string $separator
*/
protected function addScalar_path(Doctrine_Query $query, $separator=", ")
{
    $subquery = "k99.root_id=o.root_id AND k99.lft<=o.lft AND k99.rgt>=o.rgt AND k99.level<=o.level" ;

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

Note: "o" alias is used for primary query. This code would allow me to use

{foreach .... as $appartment}
   {$appartment->path}
...

Which would print:

Australia, Victoria, Melbourne, ...other children...

How to do the same thing in D2? And how to even include doctrine extenstions in my symfony2 project?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use it in QueryBuilder you must :

1) add DQL functions GroupConcat: GroupConcat

2 ) Registering GroupConcat :DQL User Defined Functions

another alternative is to use NativeQuery :Native SQL


In symfony2 registering a function DQL it's very simple just add GROUP_CONCAT in config.yml like:

    entity_managers:
        default:
            dql:
                string_functions:
                    GROUP_CONCAT: YourBundleQueryMysqlGroupConcat

For more information visit Registering Custom DQL Functions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...