您的位置 首页 MySql

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")

解决了这个问题

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章