How to access the user model correctly from within a module

Hello,

This dilemma might be a bit hard to explain but I’ll try my best.

Currently I’m not satisfied with the way I’m accessing the application’s user model from within my module and I’m looking for good suggestions on how to do this.

My module needs to access the user model for e.g. listing users. The name of the user model can be configured in the module configuration so as long as the application has an user model everything should work fine.

The problem is that I’m not exactly sure how I should call the user model. I have a few ideas which I’m not too satisfied with:

[list=1]

[*]Create a new instance of the user model and save it in a member variable in the module class, e.g. $user or $_user (with getter and setter). Then call the user model via the module.

[*]Make a wrapper application component for the user model from which the user model can be accessed. This sounds like a long shot but it is just a crazy thought I had one day.

[/list]

The problem is that I cannot call the user class statically (e.g. User::model()->…) because this would require me to hard-code the user class name in the module source which is not an option.

I need a good and most of all clean solution to this. If you even have the wildest suggestions please do not hesitate to post it below.

Thanks for reading.


CActiveRecord::model($userModelName)->doSomething();

This looks more like the answer I was looking for. Thanks for the help. :)

I still have a problem that is kind of related to this. The problem is that the user id and user name columns can be configured in the module config as well.

Currently I save them as member variables in the module and each time I need them I get them and save them into local variable which are then used with the model objects. I don’t really like this approach but I haven’t yet thought of a better way of doing it.

Any ideas on how to do this properly?

Well, you can have a requirement to connect a behavior to your model and it will set variables as needed. Another way is to require model to implement an interface with getUserId() and getUserName().

These are both good ideas but I think the way I’m doing it now allows for very easy implementation of the module without requiring the developer to implement any interfaces or attach any behaviors. Do I have any other options in addition to these?