php - Doctrine:不能为子查询分配别名

标签 php mysql symfony1 doctrine alias

我有一个 Doctrine 查询,它在选择中有一个子查询,以便从另一个表中获取一个字段(并摆脱繁重的左连接)。

问题是 Doctrine 返回给我的别名不是我想要的,因此我很乐意自定义它们。这可能吗?

这是我的查询:

$query->select(
        "id, page_id, status, title, "
      . "segment, url, current_user_id, "
      . "current_user_last_action_time, pt.name,"
    )
        ->addSelect("(SELECT s.username from sfGuardUser s where id = current_user_id) ")
        ->innerJoin( 'PbType pt' )
        ->innerJoin( 'LatestVersion lv WITH version=lv.version' )
        ->where('is_visible = 1')
        ->groupBy( 'page_id' );

这会返回一个这样的查询:

SELECT `p`.`id` AS `p__id`, `p`.`page_id` AS `p__page_id`, 
`p`.`status` AS `p__status`, `p`.`title` AS `p__title`, `p`.`segment` AS `p__segment`,
 `p`.`url` AS `p__url`, `p`.`current_user_id` AS `p__current_user_id`, 
`p`.`current_user_last_action_time` AS `p__current_user_last_action_time`, 
`p2`.`id` AS `p2__id`, `p2`.`name` AS `p2__name`, 
(SELECT `s`.`username` AS `s__username` FROM `sfguard`.`sf_guard_user` `s` WHERE 
`p`.`id` = `p`.`current_user_id`) AS `p__0` 
FROM `personaltable`.`page` `p` INNER JOIN `personaltable`.`type` `p2` ON `p`.`type_id` = `p2`.`id` 
INNER JOIN `personaltable`.`latest_page_version` `p3` ON `p`.`page_id` = `p3`.`page_id` 
AND `p`.`version` = `p3`.`version` WHERE `p`.`is_visible` = 1 
GROUP BY `p`.`page_id`

这就是我要更改的AS p__0。有什么办法吗?

最佳答案

应该很简单

->addSelect("(SELECT s.username from sfGuardUser s where id = current_user_id) AS alias")

关于php - Doctrine:不能为子查询分配别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13838901/

相关文章:

php - 使用 PHP 从 Excel 文件 (xlsx) 中提取图像

php - App::singleton 和 bindShared 有什么区别?

php - 如何获取总数量并将其发送到另一个表(Laravel)

mysql - 使用 SQL 查询总和

mysql - Symfony - 数据库链接列

php - NSData(if 语句)

PHP 或 MySql 错误;可以在浏览器中显示 "error page"吗?

mysql - 从使用双引号作为转义字符的 CSV 文件加载数据

symfony1 - 使用 Doctrine 在 Symfony Multi-Tenancy 应用程序中进行数据分离

php - 推进和左加入