[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.