How Do I Best Enforce A Site-Wide Where Clause?

I’m guessing this is a common need.

I have a user table in MySQL that I use for authentication and authorization.

ON login, I save the ‘company_code’ that each user is associated with.

This can be then accessed as ‘Yii::app()->user->user_company_code’

For all subsequent retrievals from the system’s several tables (each of which has a ‘company_code’ value),

I want a WHERE clause item to secretly add:

. . . WHERE table_name.company_code = ‘Yii::app()->user->user_company_code’

My question is: where is the best place to add that feature?

Thanks.

See scopes, default scope.

You can extend CActiveRecord and add it there.

But if you do it system-wide, you’ll not be able to, say, update your records from adminpanel (because of extra condition).

I suppose it’s better to define a system-wide named scope (for example, “own”) and modify your code like this:

MyModel::model()->own()->findAll()