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

标签 mysql string doctrine-orm concat

我的 MySQL 表中有字段 firstnamelastname。为了方便起见,我想在我的 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 - Doctrine 2 DQL CONCAT 字段和常量字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755519/

相关文章:

php - MySQL查询具有特定字段的最后一行

mysql - 如何在mysql中使用id设置用户名

php - 刷新后不要重复插入公式数据

c++ - 无法初始化类型为 "std::string&"(非 const 限定)的引用

ruby-on-rails - rails : How to downcase non-English string?

c# - 将换行符附加到字符串的最佳方法,最后一个除外

php - 使用 Doctrine2 搜索多对多关系

mysql - 如何计算 GROUP_CONCAT 中有多少个逗号分隔值

mysql - 如何插入 YYYY-MM-DDThh :mm:ss±hh:mm date in mysql with timezone?

php - 什么相当于在条令中编写任务的存储库?