I'm developing a site with codeigniter. Now, normally when you use a class in codeigniter, you basically use it as if it were a static class. For example, if I head a model called 'user', I would first load it using
$this->load->model('user');
and than, I could invoke methods on that user class like
$this->user->make_sandwitch('cheese');
in the application that I'm building, I would like to have one UserManagement class, which uses a class called 'user'.
so that, for example I could
$this->usermanager->by_id(3);
and this would return an instance of the user model where the id is 3. What's the best way to do that?
Use abstract factory pattern or even Data access object pattern which does the job that you require.
The model classes in CI are not quite the same thing as model classes in other syntax's. In most cases, models will actually be some form of plain object with a database layer which interacts with it. With CI, on the other hand,
Model
represents the database layer interface which returns generic objects (they're kinda like arrays in some ways). I know, I feel lied to too.So, if you want to make your Model return something which is not a
stdClass
, you need to wrap the database call.So, here's what I would do:
Create a user_model_helper which has your model class:
In usermanager.php: