Search based on child properties?

New to Yii, but loving it so far. Basically I’m doing a real-estate type site where I have the following Models:

Property - Contains info such as address, name, owner information etc. (HAS_MANY Listing)

Listing - contains info such as price, number bedrooms, number bathrooms.

Property for instance would be an apartment building, and the listings would be a 1 bedroom 1 bathroom option or a 3 bedroom, 2 bath option.

So I’m trying to apply some filtering functionality to the Property index such that I can only show Properties that have listings with a specified number of bedrooms and bathrooms etc.

So far I’ve managed to get most of the way towards the filtering using some custom scopes as follows. (Property.min_price and Property.max_price being updated when listings are created/updated/deleted)

$properties = Property::model()->isVisible()->ofType($search_type)->minPrice($min_price)->maxPrice($max_price)->findAll($criteria);

but after staring at the problem for a few hours its still beyond me how to work in filtering properties based on it’s children’s # bedrooms and bathrooms. Any ideas?

Can you paste your db structure, it is hard to suggest solution without it.

Property Model Relations


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(

		'listings' => array(self::HAS_MANY, 'Listing', 'property_id'),



Here’s the structure of Listing:


 * This is the model class for table "Listing".


 * The followings are the available columns in table 'Listing':

 * @property integer $id

 * @property integer $property_id

 * @property integer $bedrooms

 * @property integer $bathrooms

 * @property integer $price

 * @property string $price_type

 * @property string $lease_type


 * The followings are the available model relations:

 * @property Property $property


This help?