I am trying to figure out how to add conditions to my model::search criteria only if a self::HAS_MANY relationship exists…
eg. if my hotelroom does not have bookings and I try to search on date availability it will not show up. Unless I have a child of Bookings with dates set.
Sorry for the confusion. I totally had the design wrong. Instead of working with Yii AR I decided to just write the SQL statement I need:
SELECT * FROM tbl_rooms
WHERE id NOT IN
(
SELECT DISTINCT room_id FROM tbl_booking
WHERE UserInput->check_in BETWEEN check_in AND check_out
OR UserInput->check_out BETWEEN check_in AND check_out
)
Now I would like to transfer this to the CDbCriteria within the Rooms Model. where
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'bookings' => array(self::HAS_MANY, 'Booking', 'room_id'),
)
}