错误:预期的已知函数,得到“GROUP_CONCAT" [英] Error: Expected known function, got 'GROUP_CONCAT'

查看:32
本文介绍了错误:预期的已知函数,得到“GROUP_CONCAT"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PhpMyAdmin 中,我可以使用以下脚本,它的作用就像魅力一样:

In PhpMyAdmin I can use the following script and it works like charm:

SELECT personal.city, GROUP_CONCAT(technologies.tech)
FROM personal
INNER JOIN technologies ON technologies.uid = personal.uid
WHERE personal.uid = 88

但我有一个动态方法,它看起来像这样:

But I have a dynamic method, which looks like this:

public function user($uid, $selection, $tables) {
    // 1. Query builder
    $queryBuilder = $this->createQueryBuilder('u')->select($selection)->where('u.uid = :uid')->addSelect('GROUP_CONCAT(t.tech)');

    // 2. Get each table
    foreach ($tables as $key => $value) {
        $queryBuilder = $queryBuilder->innerJoin('App\Entity\\' . $value, $key)->andWhere("$key.uid = :uid");
    }

    // 3. Finish and return the query
    return $queryBuilder
        ->setParameter('uid', $uid)
        ->getQuery()
        ->getResult()
    ;
}

如果我运行它,我会收到以下错误消息:

If I run it, I'll get the following error message:

[语法错误] 第 0 行,第 25 列:错误:预期已知函数,得到'GROUP_CONCAT'

[Syntax Error] line 0, col 25: Error: Expected known function, got 'GROUP_CONCAT'

GROUP_CONCAT 我到底做错了什么?

那个问题的答案对我没有帮助,因为他们的问题是指 Symfony2,但我使用的是 Symfony4.

The answers of that question did not help me, since they question refers to Symfony2 but I am using Symfony4.

推荐答案

GROUP_CONCAT 不是 DQL 函数.你不能在 DQL 中使用 MySQL 函数.以下是 Doctrine 在 DQL 中支持的函数列表:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/dql-doctrine-query-language.html#dql-functions

GROUP_CONCAT is not DQL function. You can`t use MySQL functions in DQL. Here is a list of functions that Doctrine supports in DQL: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/dql-doctrine-query-language.html#dql-functions

我强烈建议您不要在您的应用中使用 GROUP_CONCAT.您可以在单独的查询中以数组的形式获取数据,并在 PHP 中将其连接起来.应该没有明显的性能开销.

但如果您真的必须使用它,您必须编写自己的用户定义的 DQL 函数.这是它的教程:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/dql-user-defined-functions.html

But if you really have to use it you must write your own user defined DQL function. Here is tutorial for it: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/dql-user-defined-functions.html

以下是在 Symfony 中注册 DQL 函数的方法:https://symfony.com/doc/current/doctrine/custom_dql_functions.html

And here is how you register that DQL function inside Symfony: https://symfony.com/doc/current/doctrine/custom_dql_functions.html

这篇关于错误:预期的已知函数,得到“GROUP_CONCAT"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆