Daily Archives: August 7, 2013


Join multiple tables with Yii CActiveRecord

I wanted to select records from a table using an attribute in one of the relations, which meant the underlying query would need to join two tables. I initially tried this

CActiveRecord::model('Customer')->with('Company', 'Account')->findByAttributes(array('account.id' => 1));

The above does not work. The trick is you need to use CDbCriteria and findAll() like below.

$criteria = new CDbCriteria();
$criteria->condition = 'account.id = 1';
CActiveRecord::model('Customer')->with('Company', 'Account')->findAll($criteria);