Use Scopes With Many To Many

Dear All,

My problem relate to scopes with many to many relations.

My situation is this;

I have three tables, user / zone / children.

Children linked to zine via column zone_id. ( relation ‘zone’ for Children Model and ‘children’ for Zone model)

user and zone tables have many to many connection via table users_has_zone. ( many to many relation ‘user’ and ‘zone’ in respective models)

I want to restrict users to see only childrens which are linked to the zones that connected to user.

My plan is make scope in zone and apply this scope to children.

So far I tried to do this in by using defualt scope in zone as follows;

public function defaultScope()


    $user = Yii::app()->user->id;

    return array(        








a). Cgridview ( zone/admin) display all zones instead of display only linked zones to user.

B). zone/view display detail of only linked zones. For others it says ’ record not available’. ( This is correct)

c). children/admin display all records but empty cells for restricted zone ids.

Where did I went wrong?

It’s highly appriciate any support to solve this as I structed there in last four days.