学说2和group_concat

2022-01-03 00:00:00 php mysql doctrine-orm group-concat

我一直在浏览文档/谷歌,但没有找到任何解决方案.

i've been looking through the docs/google and didn't find any solution.

有没有什么方法可以在没有本地mysql的情况下使用doctrine2(DQL)来执行或模仿GROUP_CONCAT?

is there any way to execute or imitate GROUP_CONCAT using doctrine2 (DQL) without native mysql?

例如:

SELECT u.id, u.name, [GROUP_CONCAT(...)] AS user_messages
FROM modelsUsers u
LEFT JOIN modelsMessages m
GROUP BY u.id

得到

GROUP_CONCAT(m.id,'|',m.title SEPARATOR ':')

现在我正在使用 createNativeQuery() 来运行它,但我正在寻找一个 docter2 解决方案.

right now i'm using the createNativeQuery() to run it, but i'm looking for a doctrine2 solution.

提前致谢

推荐答案

是的,这可以通过 Beberlei(Doctrine2 的核心开发人员)的 DoctrineExtensions 实现.在doctrine2 中,您可以通过扩展functionNode 类来定义您自己的查询表达式.

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.

最简单的方法是在您的项目中包含 DoctrineExtensions 库.我不知道您使用的是 Zend Framework 还是 Symfony 或任何其他框架,因此无法帮助您嵌入它.

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.

您可以在此处查看 DoctrineExtensions:

You can checkout DoctrineExtensions here:

https://github.com/beberlei/DoctrineExtensions

还有群功能:

https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/GroupConcat.php

请注意,您应该使用 GroupConcat 作为函数名,而不是像 MySQL 中那样使用 GROUP_CONCAT.

Be aware you should use GroupConcat as function name not GROUP_CONCAT like in MySQL.

希望对你有帮助!

相关文章