Welcome Guest, Not a member yet? Register   Sign In
How much should models do?
#1

[eluser]brainer[/eluser]
Hi,

I'm about to re-write a CI app of mine.

In the current version - pretty much all my models do is interact with the database. i.e. here is a typical (yet simple) example of a current model:

Code:
class User_model extends Model {

    function update($data,$userid)
    {
        $this->db->where('userid', $userid);
        $this->db->update('users', $data);
    }    
    
    function get($userid)
    {
        $this->db->where('userid', $userid);
        $q = $this->db->get('users');
        if ($q->num_rows() == 1) return $q->row();
        return NULL;
    }
    
    function add($data)
    {
        if($this->db->insert('users', $data)) return true;    
        return false;    
    }    
    
    function delete($userid)
    {
        $this->db->where('userid', $userid);
        $this->db->delete('users');    
    }    
    
}

However I have read a lot that models should take care of all 'Business Logic'. I guess I'm kind of confused as to what exactly 'Business Logic' is.

For instance in my controller I currently fetch data from the model and process that data there in the controller. For example:

Code:
$user = $this->user_model->get(1);
$user->avatar = remove_ext($user->avatar) . '_small.' . get_ext($user->avatar);
$user->age = dob_to_age($user->age);

I know that CI is flexible enough for me to do whatever I want wherever I want, but it seems to me that using models just for database interaction is pretty pointless. Should I be processing the 'age' and 'avatar' in my model?

Just wondering how all of you use your models.




Theme © iAndrew 2016 - Forum software by © MyBB