我有一个链接到用户配置文件实体的用户实体。
我需要能够连接用户的名字和姓氏以进行过滤。
这大致就是我的查询的样子:
$qb = $this->createQueryBuilder('users');
$qb
->addSelect('users')
->innerJoin('users.profile', 'profile')
->addSelect('profile')
->addSelect('CONCAT(profile.firstName, \' \', profile.lastName) AS fullName')
;
我希望将全名视为用户属性或个人资料,但我在结果中将全名作为一个全新的记录获取:
[0] => Array (
[id] => 5
[username] => admin
[profile] => Array (
[firstName] => John
[lastName] => Doe
)
),
[fullName] => John Doe
我尝试将该列别名为“users.fullName”,但这给了我一个错误。
[语法错误]第 0 行,第 87 栏:错误:预期 Doctrine\ORM\Query\Lexer::T_FROM,得到“.”
执行此操作的正确方法是什么?
谢谢
最佳答案
为什么不在 select 方法中提供所需列的完整列表:
->select("user.id, user.username, CONCAT(profile.firstName, ' ', profile.lastName) AS fullName")
您应该收到正确的关联数组。
记住添加适当的 where 子句:
->where("CONCAT(profile.firstName, ' ', profile.lastName) LIKE :name")
->setParameter('name', '%' . $name . '%')
关于php - Doctrine DQL - 选择添加为新记录的列别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782107/