0

我有这个查询,我想用 CDBCriteria 写它。可以用 CDBCriteria 写吗?我不知道如何在 Yii 框架中使用 CDBcriteria 编写此查询。

谢谢你的时间。

    $opt='';
    if(!empty($roomtype))
    {
        $opt .= " AND id_room_type = ".$roomtype;
    }
    if(empty($children))
    {
        $opt .= " AND c.childrenFriendly = 1 ";
        $capacity = $adults + $children;
    }
    else {
        $capacity = $adults;
    }
    if(!empty($seaview))
    {
        $opt .= " AND c.seaview = 1 ";
    }
    if(!empty($smoking))
    {
        $opt .= " AND c.smoking = 1 ";
    }
    if(!empty($king_beds))
    {
        $opt .= " AND c.king_beds >= 1 ";
    }

    $qry = RoomType::model()->findAllBySql("
        SELECT (number_of_rooms) AS total, id_room_type, c.reservation, c.start, c.end, childrenFriendly, smoking, king_beds, capacity,
        SUM( number_of_rooms - IFNULL( c.reservation, 0 ) ) AS result
        FROM room_type
        LEFT JOIN (
          SELECT fk_room_type, COUNT( fk_room_type ) AS reservation, start ,end
          FROM room_type_reservation
          WHERE
          (end > '".$start->asSql()."' AND start < '".$end->asSql()."') OR
          (start < '".$end->asSql()."' AND end < '".$start->asSql()."')
          GROUP BY fk_room_type
        )c ON id_room_type = fk_room_type
        WHERE enabled = 1 AND capacity > ".$capacity." ". $opt ."
            GROUP BY id_room_type
            HAVING result > ".$number_of_rooms."
    ");
    return $qry;
4

1 回答 1

0

当您阅读laser_wizard 引用的文档时,请特别注意命名范围。它们不是您在编程中想到的范围的范围。相反,它们是一种将预定义表达式添加到 where 子句的方法。您可能还想考虑在 SQL 中使用参数标记而不是变量名。这样做将有助于保护您免受 SQL 注入。

于 2013-06-13T20:26:48.097 回答