我有一个doctrine查询,在select中有一个子查询,以便从另一个表中获取一个字段(并摆脱一个沉重的左连接)。
问题是,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
我想要改变的是什么。有没有办法做到这一点?
答案 0 :(得分:0)
应该像
一样简单->addSelect("(SELECT s.username from sfGuardUser s where id = current_user_id) AS alias")
答案 1 :(得分:0)
前段时间我遇到了同样的问题,刚刚发现一个有效的解决方案,但是没有使用orm。 通过你当前的学说连接使用真正的mysql来尝试它。
$query = 'SELECT p.id, p.page_id, p.status, p.title, p.segment, p.url, p.current_user_id,
p.current_user_last_action_time, p2.name,
( SELECT s.username from sfGuardUser s where id = p.current_user_id ) as XXX
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';
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
$statement = $connection->execute($query)->fetchAll();
foreach ($statement as $key => $val) {
$id = $statement[$key]['XXX'];
}
XXX是您不想自定义的别名。
我知道这不是好风格,但我无法以任何其他方式做到这一点。