AR stretched out Relation

Ok, so I’m simplifying, but I have an interesting relationship that I can seem to find any discussion of.









I want to set up a relationship in my Person record that gives me the Division they belong to. Any ideas? I already have the department relationship. That’s easy: array(self::BELONGS_TO, ‘Department’, ‘department_id’).

I don’t know if this can be done directly in the Person model.

But you can define a relation in Departament model, like this:


Then, you can obtain the Division of some person doing this:

$div = $person->departament->division //Where $person is an instance of Person model.

Yeah, I do this in several places, but I think I actually need a relationship like this for the search feature in my grid view. I have a gridview for Person where I added the column for division.









Then in Person->search()



$criteria->alias = 'person';

$criteria->join = 'left join department on person.department_id = department.department_id left join division on department.division_id = division.division_id';

If I leave Division out of the grid, I can just use $criteria->with = array(‘department’). But when I want to talk about Division, I need to join to it, and if I have with(‘department’) and try to mention department in the join line, it doesn’t know what I’m talking about, so I don’t use with and just specify the whole join myself.

I figure if I can get a relationship in Person for this Division, then I can do with = array(‘department’,‘division’) and go on my merry way, since this works for department.



WOW, that is awesome and it works! Thanks so much. I didn’t know you could do that.

Yeah… Yii is very powerfull