【问题标题】:Concat three or more fields in doctrine在学说中连接三个或更多领域
【发布时间】:2014-04-05 19:59:20
【问题描述】:

Doctrine 查询生成器只允许我连接两个字段。

class Expr {
// ...
public function concat($x, $y); // Returns Expr\Func

要连接我使用的 3 个字段:

$qb->expr()->concat(
    'table.field1',
    $qb->expr()->concat('table.field2', 'table.field3')
);

SQL 将是:

CONCAT('table.field1', CONCAT('table.field2', 'table.field3'))

如何获得一个 concat?

当我尝试直接调用时

new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));

执行查询给我一个错误

[语法错误] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

转储 DQL:

CONCAT('table.field1', 'table.field2', 'table.field3')

使用 $qb->getQuery()->getSQL() 转储 SQL:

[语法错误] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

【问题讨论】:

标签: symfony doctrine-orm


【解决方案1】:

CONCAT 已在Doctrine ORM version 2.4.0 中引入了可变数量的参数,因此您可能使用的是旧版本。我已经在我的项目中测试了类似的 DQL,并且按预期工作。

你应该升级你的 Doctrine ORM 部门:

php composer.phar require doctrine/orm:~2.4

请注意,(目前)变量参数仅由 Expr\Func 构造函数支持,但在 Expr::concat 方法中不支持;你仍然需要这个语法:

new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));

更新我创建了一个PR on GitHub 来支持Expr::concat 中的多个参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多