I've been struggling with an application (model) design problem for the past couple days, and can't really say that I've made much progress. I'm new to PHP frameworks. I've have been using CodeIgniter for last two months. Everything is fine so far, but as the count of entities (tables) in the app increases, I begin to wonder if I'm doing things right.
I'm not quite sure about the difference between Model and a Custom Result Object.
Let's say I have two base tables (users
and roles
) and an associative table (user_role
). For the users
table, I want to be able to use the following actions:
- create, get (user), get (users), update, delete
And for the roles
table:
- create, get (role), get (roles), update, delete
And for the relation between the two user_role
:
- assign role (to a user), remove role, get role
If I were to create a User_model
, how should it look? I could define methods such as
public function get_user($user_id) {
return $this->db->from("users")->where("id", $user_id)->get()->row_array();
}
but I don't want to work with arrays. I mean, then, how do I get the user's role? Another method?:
public function get_user_role($user_id) {
return $this->db->query("SELECT * FROM roles WHERE id IN (SELECT role_id FROM user_role WHERE user_id = ?)", $user_id)->row_array();
}
I'd have to do:
$this->load->model("user_model");
$user = $this->user_model->get(1); // array
$user_role = $this->user_model->get_user_role($user["id"]); // array
What if I have more entities (that's related to users
)?
$user_permissions = $this->user_model->get_user_permissions($user["id"]); // array
$user_products = $this->user_model->get_user_products($user["id"]);
$user_tasks = $this->user_model->get_user_tasks($user["id"]);
// etc.
This looks messy, and like a bad design. If I want to use an object that represents a table row, and add all other relations to the object, how would I design it? I mean, should I add every needed properties/methods inside User_model
and when I get a row ($this->user_model->get(1)
) from the table, return a User_model
(with custom_row_object(0, "User_model")
) instead of an array (row_array()
)? Or should the object that represents a table row be a different object than User_model
?
For example:
// model is instantited and accessible at $this->user_model
// since it's automatically instantiated (when loaded), it's not linked to any row
// this model could act as a intermediary between the controller and the database/table
// if it will never be linked to a row, there's no reason to have update, delete, etc. methods on the model
// model's role is to insert rows and get objects that represent table rows
$this->load->model("user_model");
// when requested a table row, we return an object that represents a table row
$user = $this->user_model->get(1); // $user is an object; ...->get()->custom_row_object(0, "UserRow")
$user->get_role(); // role is accessible at $user->role; (this could just be another object: a role object)
$user->role->delete();
$user->get_permissions(); // again, $this->permissions; (could be an object)
This way, I could collect all other entities that's related to a user in a single object. The problem with this is, should the returned object be the model (custom_row_object(0, "User_model")
) or a completely different object, e.g., UserRow
(custom_row_object(0, "UserRow")
)?
If I use a different object, say UserRow
, that means User_model
won't have much properties/methods, that it can only be used when inserting a new row, or getting row(s) from the table (+ maybe store helper methods). I could add the update, delete, get (associated entities) methods to the second (UserRow
) object.
I'm really at loss here. I need some pointers.
I know this doesn't seem like a real programming question, and probably opinion-based, but I really need help :|