Find ActiveRecord by it's relations key match?


Is there a way to find ActiveRecords by relations key match?

I have model

class Business extends CActiveRecord


    public function relations()


        return array(





What I'm trying to do is:

$businesses=Business::model()->with('addresses')->findAll("addresses.address1=?",array('Yii Street'));

So, basically I need all business with addresses where address1='Yii Street'.

address1 is a field of BusinessAddress table.


you need to use: with(…)->together()->findAll…

Here is how it worked:

$businesses=Business::model()->with('addresses')->together()->findAll("t1.address1=?",array('Yii Street'));


is there a way to use something instead of t1? maybe a method or?


You need to set 'alias' option in the relation declaration. Otherwise, it will use an automatically generated alias.

Thank you qiang.

Your participation in forum's life and your help was always impressing me.