Doctrine DBAL查询构建器省略了一些连接

时间:2011-09-07 08:50:57

标签: doctrine-orm symfony

PHP代码:

    $xCodesQueryBuilder = $conn->createQueryBuilder();

    $xCodesQueryBuilder->select("l.id","mdsh.xcode","mdso.xcode")
            ->from("location_tree","l")
            ->join("l","location_tree_pos","p","l.id = p.tree_id")
            ->rightJoin("l","hotel","h","h.location_id = l.id")
            ->leftJoin("l","offer_location","ol","l.id=ol.location_id")
            ->leftJoin("ol","mds_offer","mdso","ol.offer_id = mdso.offer_id")
            ->leftJoin("h","mds_hotel","mdsh","h.id = mdsh.hotel_id")
            ->where("p.parent_id IN (:ids)")
            ->andWhere("(mdso.xcode IS NOT NULL OR mdsh.xcode IS NOT NULL)");

    var_dump($xCodesQueryBuilder->getSQL());exit;

结果:

SELECT l.id, mdsh.xcode, mdso.xcode
FROM location_tree l 
INNER JOIN location_tree_pos p ON l.id = p.tree_id 
RIGHT JOIN hotel h ON h.location_id = l.id 
LEFT JOIN offer_location ol ON l.id=ol.location_id 
WHERE (p.parent_id IN (:ids)) 
AND ((mdso.xcode IS NOT NULL OR mdsh.xcode IS NOT NULL))

为什么2个最后加入的任何想法都被省略了?

2 个答案:

答案 0 :(得分:3)

我刚刚为我工作了。必须修改getSQLForSelect()中的函数QueryBuilder.php

我打开了一个ticket并向DBAL提交了拉取请求,但与此同时,请随时使用my patched copy

<强>更新

刚刚意识到解决此问题的另一种方法是始终在任何FROM方法中使用$fromAlias表别名作为第一个参数(join())。

在您的情况下,您将代码更改为:

$xCodesQueryBuilder = $conn->createQueryBuilder();

$xCodesQueryBuilder->select("l.id","mdsh.xcode","mdso.xcode")
        ->from("location_tree","l")
        ->join("l","location_tree_pos","p","l.id = p.tree_id")
        ->rightJoin("l","hotel","h","h.location_id = l.id")
        ->leftJoin("l","offer_location","ol","l.id=ol.location_id")
        ->leftJoin("l","mds_offer","mdso","ol.offer_id = mdso.offer_id")
        ->leftJoin("l","mds_hotel","mdsh","h.id = mdsh.hotel_id")
        ->where("p.parent_id IN (:ids)")
        ->andWhere("(mdso.xcode IS NOT NULL OR mdsh.xcode IS NOT NULL)");

var_dump($xCodesQueryBuilder->getSQL());exit;

答案 1 :(得分:0)

我认为Doctrine 2.3.1支持这一点。

可以将连接转换为from和where,如果需要,可以使用group by。 卡住了2-3个小时,最后使用了这个解决方法 因为即使升级后也没有帮助我(试图找出原因)