Question

I am using one table users[id,user_type,...] for three controllers (users, tenants, clients)
In the last two models (Tenant & Client), I use the $useTable='users'.

What i need is a way to automatically filter every model operation, based on the User.user_type value.

Meaning: tenants and clients controllers will execute the same methods, but their models will add a condition to each and every query they execute.
(with simple find() queries it's simple - i will just add it as a condition in the beforeFind() call, but how can it be done for mass delete or mass update?)

Was it helpful?

Solution

Create a class that extends the AppModel like UserWrapper.

Then your three models should extend the UserWrapper instead of AppModel.

In the UserWrapper overwrite the appropriate methods to add the filtering you require.

You could use instanceof to determine what type of user you're looking at from inside the UserWrapper class and add the required user_type conditions to the query. And then you call the parent method.

It may be tricky to get it right, but it should work and be transparent for the controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top