doctrine2和group_concat [英] doctrine2 and group_concat
问题描述
我一直在浏览docs / google,没有找到任何解决方案。
有没有办法执行或模仿GROUP_CONCAT使用doctrine2(DQL)没有本机mysql?
例如:
SELECT u.id ,u.name,[GROUP_CONCAT(...)] AS user_messages
FROM models\Users u
LEFT JOIN models\Messages m
GROUP BY u.id
获取
GROUP_CONCAT(m.id,'|',m.title SEPARATOR':')
'使用createNativeQuery()来运行它,但是我正在寻找一个doctrine2解决方案。
提前感谢
是可以使用来自Beberlei(Doctrine2的核心开发人员)的DoctrineExtensions。在doctrine2中,您可以通过扩展functionNode类定义自己的查询表达式。
最简单的方法是将DoctrineExtensions lib包含在项目中。我不知道你使用Zend Framework或Symfony或任何其他框架,所以无法帮助你嵌入它。
你可以在这里结帐DoctrineExtensions: p>
https://github.com/beberlei/DoctrineExtensions
而群组功能:
https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/GroupConcat.php
请注意,您应该像在MySQL中一样使用GroupConcat作为函数名称而不是GROUP_CONCAT。
希望它有帮助你!
i've been looking through the docs/google and didn't find any solution.
is there any way to execute or imitate GROUP_CONCAT using doctrine2 (DQL) without native mysql?
eg.:
SELECT u.id, u.name, [GROUP_CONCAT(...)] AS user_messages
FROM models\Users u
LEFT JOIN models\Messages m
GROUP BY u.id
to get
GROUP_CONCAT(m.id,'|',m.title SEPARATOR ':')
right now i'm using the createNativeQuery() to run it, but i'm looking for a doctrine2 solution.
thanks in advance
Yes this is possible with DoctrineExtensions from Beberlei (a core developer of Doctrine2). In doctrine2 you can define your own query expressions by extending the functionNode class.
The easiest way is to include the DoctrineExtensions lib in your project. I don't know wether your are using Zend Framework or Symfony or any other framework so can't help you with embedding it.
You can checkout DoctrineExtensions here:
https://github.com/beberlei/DoctrineExtensions
And the group function:
https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/GroupConcat.php
Be aware you should use GroupConcat as function name not GROUP_CONCAT like in MySQL.
Hope it helps you!
这篇关于doctrine2和group_concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!