symfony Doctrine DQL -选择作为新记录添加的列别名

kb5ga3dv  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(101)

我有一个链接到用户配置文件实体的用户实体。
我需要能够连接一个用户的名字和姓氏的过滤目的。
我的查询大致如下所示:

$qb = $this->createQueryBuilder('users');
$qb
    ->addSelect('users')
    ->innerJoin('users.profile', 'profile')
    ->addSelect('profile')
    ->addSelect('CONCAT(profile.firstName, \' \', profile.lastName) AS fullName')
;

我希望fullname被视为用户属性或配置文件,但实际上,我在结果中得到的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,却得到'.'

正确的做法是什么?
谢谢

cngwdvgl

cngwdvgl1#

为什么不在select方法中提供所需列的完整列表:

->select("user.id, user.username, CONCAT(profile.firstName, ' ', profile.lastName) AS fullName")

您应该会收到正确assoc数组
请记住添加适当的where子句:

->where("CONCAT(profile.firstName, ' ', profile.lastName) LIKE :name")
    ->setParameter('name', '%' . $name . '%')

相关问题