Multiple Many::many Relations. In A Cgrid

I’ve looked and searched and just can’t figure out how the “Yii” way of doing the following would be. I say Yii way because I find that I can force it to do something but their is usually a simpler better easier way to do it.

I have in this case 4 Models. The first links to the second which links to the third which links to the forth. If I’m working in the first how do I filter/sort based on something in the forth.

Contacts -> Relationship -> Join Table -> Address. Is what I’m working with. So I have some contacts they have a relationship to one another based on the Relationship table. The relationship table joins multiple Address which can be reused via a Join Table. I want to filter the Contacts based on the State in the address.

I’ve searched for multiple models, chaining models, join tables yada yada. But my google foo seems to be lacking. Is this really that odd of a database setup?

I mean I can think of a bunch of simple examples. What if I were a car dealership. I would have a list of cars that list would have a list of models under it and the models would be package options and under the options would be a list of individual features. If I wanted to get a list of all cars with moon roofs how would I do that?

Do I have to build a complex set of relations that says join car to models and then a relation for packages to car through models then a relation for options through models and then then a relation for features through options? Then just say $model->options($someCriteria); and it would join all of them together?

Look at my extension www.yiiframework.com/extension/relatedsearchbehavior/ - there is a demo with it which should allow you to learn how to do it.