I’m just wondering which one is better, or more recommended for processing form data.
// Controller
// validation etc
$data = array('name'=>$this->input->post('name'), '... etc');
$this->user_model->insert($data);
// Model
function insert($data){
$this->db->insert('users',$data);
}
or
// Controller
// validation etc
$this->user_model->insert();
// Model
function insert($data){
$data = array('name'=>$this->input->post('name'), '... etc');
$this->db->insert('users',$data);
}
I’m using the second method at the moment. Despite no problem for inserting, I have to say it’s not very suitable for updating. I end up having to create new methods to update only certain fields of a table. I’m thinking of changing to the first method. I’m just wondering whether anyone can help give clearer pros and cons of each method.
Thanks!
I agree that Models and Libraries should be doing most of the heavy processing, however I believe that Model and Library methods should be as re-usable as possible, and the Controller should act as traffic director, deciding which models to call and what data to send to them.
So in your example, the former makes more sense. Think about it this way — isn’t it possible that you may want to add a user some other way, other than via form posts?
Another option, if you want best of both worlds, create a wrapper function in the model:
Of course in this example it seems excessive. But in more complex applications you quickly benefit from being able to reuse functions that have established, predictable and tested results.