Reputation: 3329
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.
Upvotes: 1
Views: 1013
Reputation: 58444
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 another CI_Model
instance, because it does no have such capabilities CI_Controller
.
Instead you would have to override default __construct()
, which would access the global instance of CI and bind it to $this->load
variable. 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.
Upvotes: 4
Reputation: 779
Check out DataMapper ORM
DataMapper ORM for CodeIgniter
From the site:
DataMapper is an Object Relational Mapper written in PHP for CodeIgniter. It is designed to map your Database tables into easy to work with objects, fully aware of the relationships between each other.
Upvotes: 0