I have this query and I want to write it with CDBCriteria . Can it be written with CDBCriteria? I dont know how to write this query with CDBcriteria in Yii framework .
Thanks for your time.
$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;
when you are reading the documentation referenced by laser_wizard, pay particular attention to named scopes. They aren't scopes in the way you would think of scope In programming. Instead they are a way to add predefined expressions to your where clause. you might also want to consider using parameter markers instead of variable names In your SQL. Doing that will help protect you from SQL injection.