mysql – Doctrine 2 DQL CONCAT字段和常量字符串

我在MySQL表中有字段firstname和lastname.为方便起见,我想在我的Doctrine 2实体中添加一个名为full_name的计算列.在普通的MysqL中我会做这样的事情

SELECT CONCAT(firstname," ",lastname) AS full_name FROM customers;

但是,连接字段和常量字符串(在这种情况下为“”)似乎不适用于Doctrine的CONCAT实现.使用以下代码

$repository
    ->createqueryBuilder('customer')
    ->select('CONCAT(customer.firstname,customer.lastname) AS full_name')
    // ...

我收到了错误

[Syntax Error] line 0,col 91: Error: Expected StateFieldpathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression,got '"'

我怎样才能实现与MysqL相同的行为?

最佳答案
显然,DQL中的字符串只能用单引号封装,而不能用双引号封装.文档中的简短搜索并未直接提及此行为,但我注意到所有包含常量字符串的示例都使用单引号.

更改

->select('CONCAT(customer.firstname,customer.lastname) AS full_name')

->select('CONCAT(customer.firstname,\' \',customer.lastname) AS full_name')

要么

->select("CONCAT(customer.firstname,' ',customer.lastname) AS full_name")

解决了这个问题

相关文章

MySQL 死锁 是指两个或多个事务互相等待对方持有的锁,从而导...
在MySQL中,InnoDB引擎通过Next-Key Locking技术来解决幻读问...
在数据库事务管理中,Undo Log 和 Redo Log 是两种关键日志,...
case when概述 sql语句中的case语句与高级语言中的switch语句...
其实很简单,只是为了忘记,做个记录,用的时候方便。 不管是...
1.进入服务,找到mysql服务,在属性里找到mysql的安装路径 2...