我的 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 - Doctrine 2 DQL CONCAT 字段和常量字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755519/