Active relations question

Hi all, I’m a little bit stuck on this one. I really like the active relations, once they are set up, they can really simplify your work, but perhaps I can’t make them work for this case, let me explain.

I have two tables CatalogItem and ProfileItems these two tables relate this way, other fields are removed.

CatalogItem has various fields but for this example we only need to consider the id field which is the primaryKey.

Users can set up profiles, the CatalogItem(s) are either a job requirement or a job offering (a service).

ProfileItems is essentially a join table. Fields of interest in ProfileItems are type and item. Item points directly to the CatalogItem->id and ‘type’ can be either 1 or 2 depending on if the service is required or offered.




CatalogItem  ProfileItems

id         <--- item

                type either 1 or 2 



configuring my relations in CatalogItem I can get all of the profiles that point to the catalog item specified.




'profileItems'=>array(self::HAS_MANY,'ProfileItems','item')

// or for clarity

'profileItems'=>array(self::HAS_MANY,'ProfileItems',array('item'=>'id')),



So this works




$profilesPointingToCatalogItem = CatalogItem::model()->findByPk($id)->profileItems;



But how can I filter for the ‘type’ field? for example I want to find all of the profiles that are service providers (type = 1) that offer a particular CatalogItem?

Is this possible in a relation?

Thanks,

doodle

with lazy loading:




$profiles = $catalogItem->profileItems(array('condition' => 'type = 1'));



with eager loading:




$catalogItem = CatalogItem::model()->findByPk($id, array(

  'with' => array('profileItems' => array('condition' => 'type = 1')),

));

$profiles = $catalogItem->profileItems;



with multiple relation definitions:




'profileItems' => array(self::HAS_MANY,  'ProfileItems', 'item'),

'profileItemsProvideService' => array(self::HAS_MANY,  'ProfileItems', 'item', 'on' => 'type = 1'),


$profiles = $catalogItem->profileItemsProvideService;



@phtamas Awesome!, that is exactly what I need.

Thank you for the very thorough response.

++

doodle