model::search if relationship exists.

Hello,

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.

Bump, still haven’t figured this one out. I hope an experienced user can show some light.

Right now the only idea I have is by registering a booking that has already past, however this would not be the ideal setup.

Hi add like below

array(

		'name'=>'Field_name',


		'value'=>' $model->relation_name[0]->fieldName" ',						


	),

Please post your controller, model, and view relevant code parts.

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'),

                 )

}



What I have came up with is

$criteria->addNotInCondition(‘t.id’, $id_array);

where $id_array is generated from another SQL query.

I would prefer to run this as one query though. Any thoughts of doing this with performance and security in mind?