Welcome Guest, Not a member yet? Register   Sign In
Models interacting with the database
#1

[eluser]Freeze Dried Pop[/eluser]
Can someone explain to me exactly what a model is designed to do? It seems that most people use it to interact with the database and have specific models designed to work with specific tables in the database and create a MY_Model designed specifically to include functions for working with this specific table.

Yet one point I often hear made is that we shouldn't be doing things like validation in the controller but in the models. Infact you really shouldn't be doing anything in the controller.

But what if you are doing some validation for information that is not appearing in the database, such as validating a contact form that just sends an email? Or perhaps not doing any validation at all, but simply some page that does not interact with the database.

If we should be doing all the hard work in a model rather than the controller then why do people fill their base model class up with so many functions designed for database interaction?

At the very least wouldn't it be better to seperate the DB relates models into a db folder extending a specifically designed base model (e.g. db_model rather than my_model). That way there could be multiple base models designed for whatever the models purpose is.

Code:
$this->load->model('db/users_table');

class Users_table extends db_model{}

What I get more confused about is when it's technically right to be using a library.

Take the scenario of a contact form which does no database interaction, I want to receive the data into my controller. Now I could:

Do all the work in the controller:

Code:
class Contact extends CI_Controller {
    public function index()
    {
        $errors = FALSE;
        
        if(isset($_POST['message']))
        {
      $this->load->library('form_validation');
            $this->load->helper('library');
            
            $this->form_validation->set_rules('name', 'Name', 'required|trim');
            $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
            $this->form_validation->set_rules('subject', 'Subject', 'required|trim|htmp_escape');
            $this->form_validation->set_rules('message', 'Message', 'required|trim|htmp_escape');
            
            if($this->form_validation->run() == FALSE)
      {
       $this->load->library('email');
                
                $this->email
                    ->from($this->form_validation->set_value('email'), $this->form_validation->set_value('name'))
                    ->to(WEBMASTER_EMAIL)
                    ->subject($this->form_validation->set_value('subject'))
                    ->message($this->form_validation->set_value('message'))
                    ->send();
                
                $this->load->view('contact_form_success');
                exit;
      }
      else
      {
       $errors = validation_errors();
      }
        }
        
        $this->load->view('contact_form', array('errors'=>$errors));
    }
}

But apparently this is frowned upon. So maybe I should use a model:

Code:
class Contact extends CI_Controller {
    public function index()
    {
        $errors = FALSE;
        
        $this->load->model('contact_form');
        
        if($this->contact_form->run())
        {
            $this->load->view('contact_form_success');
        }
        else
        {
            $this->load->view('contact_form', array('errors'=>$this->contact_form->errors));
        }
    }
}

class contact_form extends MY_Model {
    public $errors = FALSE;
    
    public function run()
    {
        if(isset($_POST['message']))
        {
      $this->load->library('form_validation');
            $this->load->helper('library');
            
            $this->form_validation->set_rules('name', 'Name', 'required|trim');
            $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
            $this->form_validation->set_rules('subject', 'Subject', 'required|trim|htmp_escape');
            $this->form_validation->set_rules('message', 'Message', 'required|trim|htmp_escape');
            
            if($this->form_validation->run() == FALSE)
      {
       $this->load->library('email');
                
                $this->email
                    ->from($this->form_validation->set_value('email'), $this->form_validation->set_value('name'))
                    ->to(WEBMASTER_EMAIL)
                    ->subject($this->form_validation->set_value('subject'))
                    ->message($this->form_validation->set_value('message'))
                    ->send();
                
                return TRUE;
      }
      else
      {
       $this->errors = validation_errors();
      }
        }
        
        return FALSE;
    }
}

Not that much different, maybe this is okay, but why am I extending MY_Model and pulling all those extra functions for?

Also what if I have multiple types of forms, maybe i'd like some base functions to make my life easier, but putting them in MY_Model doesn't seem to work as that has all the extra stuff for interacting with the database and it would just get too confusing to put more in.
#2

[eluser]Freeze Dried Pop[/eluser]
Apparently there's a character limit.......

So what if I use a library instead:

Code:
class contact_form {
    public $errors = FALSE;
    
    public function __construct()
    {
        $this->CI = get_instance();
    }
    
    public function run()
    {
        if(isset($_POST['message']))
        {
      $this->CI->load->library('form_validation');
            $this->CI->load->helper('library');
            
            $this->CI->form_validation->set_rules('name', 'Name', 'required|trim');
            $this->CI->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
            $this->CI->form_validation->set_rules('subject', 'Subject', 'required|trim|htmp_escape');
            $this->CI->form_validation->set_rules('message', 'Message', 'required|trim|htmp_escape');
            
            if($this->CI->form_validation->run() == FALSE)
      {
       $this->CI->load->library('email');
                
                $this->CI->email
                    ->from($this->CI->form_validation->set_value('email'), $this->CI->form_validation->set_value('name'))
                    ->to(WEBMASTER_EMAIL)
                    ->subject($this->CI->form_validation->set_value('subject'))
                    ->message($this->CI->form_validation->set_value('message'))
                    ->send();
                
                return TRUE;
      }
      else
      {
       $this->errors = validation_errors();
      }
        }
        
        return FALSE;
    }
}

This isn't too different from using a model but its annoying to have to use $this->CI, also there's no extendibility if I want to add some base functions to make my life easier.





It seems doing it in the controller is easiest, but everyone seems to lament this. Perhaps using a model would work, but I don't want to mix everything in with all database orientated functions.

So why do the same people telling me to use models and not controllers also tell me to fill my models up with things that will only be relevant for some models? Wouldn't be better to have a seperate base model class for each "type" of model and extend each only when its actually needed?

If so, why doesn't anyone say or do this? Am I completely missing something? Am I using models and libraries in the wrong way?

I've never really understood when its best to use each and have kind of just been blagging my way through, but some insight would be much appreciated.

[/newby]
#3

[eluser]Samus[/eluser]
You're meant to validate user input from the controller imo.

My personal approach would be to use a controller to validate and send the email. If you need to store information from the contact form (or just feel like keeping a log), you create a model method and call that from within your controller.

There are situations when it will be more suitable for creating a library. Such as for authenticating users, but for a simple contact form, i'd just stick to the controller.
#4

[eluser]Aken[/eluser]
I do form validation in the controller unless it's something that I use repeatedly throughout an application; then I will create a model or library for it, depending on the situation.

Models for me are database interaction, and occasionally some methods for manipulating data into a proper format.




Theme © iAndrew 2016 - Forum software by © MyBB