Model -> Search -> Long Relation


Let’s say I have the situation:

A company has departments and departments have employees

Each respective table has Id primary key and relations are defined by foreign keys using these Id numbers.

Now I have a CGridView of employees and I would like to show name of department he/she is working in (that is easy) as well as name of the company he/she belongs (difficult).

Well - in a grid I can show:

array('name'=>'company_name', 'value'=>'$data->department->company->name'),

This is showing company names appropriately but no joy with filtering/sorting !!!

I am unable to construct working seach criteria in a model. Whatever I try - I get kind of ‘column not found’ SQL error.

So - how to overcome? It’s urgent!

Thanks ahead for suggestions!

Your problem is not that much clear to me, I think you are unable to sort and filter using relations.

Use of "with" clause can sort out your problem.

Take a look over here

How to use ‘with’ clause for relations



Hi Maclein,

For simplification:

I am looking for Yii criteria syntax for:


FROM `employees` `t`

LEFT OUTER JOIN `departments` `department`

ON (`t`.`department_id`=`department`.`id`) 

LEFT OUTER JOIN `companies` `company`

ON (`department`.`company_id`=`company`.`id`)

WHERE (`company`.`name` LIKE 'Hewlett%');

Actually, I have a problem with “WHERE” condition - I can’t find good way to put it into search criteria.

All right - finally I’ve got it:

$criteria->compare(’’,$this->comp_name, true);

$criteria->with=array(‘department’, ‘’);