我刚刚遇到了同样的问题,我用下一个方法解决了:
$result = $this->createQueryBuilder('comments')
->select([
'comments.id as id',
'comments.post_id as postId',
'comments.userId as userId',
'comments.userName as userName',
'comments.userEmail as userEmail',
'comments.parentId as parentId',
'comments.postedAt as postedAt',
'comments.status as status',
'comments.comment as comment',
'user.fullname as user_userName',
'user.email as user_email',
])
->leftJoin(
'Entity\User', 'user',
Query\Expr\Join::WITH,
'comments.userId = user.id'
)
->where('comments.post_id=:postId')->setParameter('postId', $postId)
->getQuery()
->getResult(Query::HYDRATE_ARRAY)
;
因此,要为表数据添加前缀,只需将它们声明为“select”语句即可
table.var1 as prefix_var1, table.var2 as prefix_var2
结果是
[
prefix_var1 => value,
prefix_var2 => value,
]
当您将 select 声明为时,您可以做的另一件事是:
table1, table2.var1 as prefix_var1, table2.var2 as prefix_var2
你会得到下一个结果
[
0 => [ // it's come from table1
var1 => value,
var2 => value,
],
prefix_var1 => value, // it's come from table2
prefix_var2 => value, // it's come from table2
]