I have an architectural question that has been bothering me.
Codeigniter defines there model as such. Now there are times when a model is closely tied to another. I.e. User and Security.
Is it bad practice to access another model within a model?
i.e.
class User extends CI_model
{
function getUser()
{
$this->load->model('User');
$user = // load user
$this->load->model('Security');
$security = // load security
$user->security = $security;
}
}
Some how it doesn’t feel ‘kosher’ to me. It feels like I need to have a DAO or some sort of DAL on top which can then load both models and do the necessary fetching while leaving the basic CRUD and model specific operations in the model. And if we do create a separate layer, there doesn’t seem like a logical place to put them in the Codeigniter framework.
Here is the sad part: CodeIgniter does not implement MVC design pattern. It just mimics Rails architecture. There is a difference.
What you call “models” in CodeIgniter, are usually implementation of active record (anti)pattern. They combine the responsibilities of domain business logic with storage logic.
So, if you are working with standard CI code, then you are not supposed to do it. You cannot call
$this->load->model()within anotherCI_Modelinstance, because it does no have such capabilitiesCI_Controller.Instead you would have to override default
__construct(), which would access the global instance of CI and bind it to$this->loadvariable. Basically it would be a hack.You will be better off instantiating the “models” in the controller and injecting those dependencies manually.
Then again .. you are using CodeIgniter, so who cares about development principles.