Welcome Guest, Not a member yet? Register   Sign In
Model: Strictly for database INSERTS, SELECTS, UPDATES, etc?


Just wondering if "models" should only be used for general database queries, and nothing else?

The CodeIgniter user manual gives what seems to be a peculiar example of a model:

function insert_entry()
$this->title = $_POST['title']; // please read the below note
$this->content = $_POST['content'];
$this->date = time();

$this->db->insert('entries', $this);

From what I am learning about MVC, it doesn't seem right that a model's functions should be accessing the $_POST variable - it should only be using values that get explicitly passed to it?

Shouldn't it be the Controller's responsibility to grab the values of $_POST['title'] and $_POST['content'], and then pass them to the function insert_entry()? Or am I not understanding something about what should go in a model?


[eluser]Banana Man[/eluser]
The example you've shown is precisely that - an example

I would suggest that your model contains purely code that is associated with the data source (eg. database) you are dealing with.

I sometimes think of it like this, although there are many definitions

Model = Data
Controller = Logic
View = Presentation

Hope this clarifies it for ya

Exactly. For me, the model also does any data manipulation. For instance, one of my forms is an upload form, and in the model i have a custom function that uses the unix backend to check its mime type.

For me, the controller is the glue between the model and view, and also provides data validation. Any interaction done with saving, getting, or putting is all in the model though.

[eluser]Colin Williams[/eluser]
Models can also connect with Web Services, scan directories and files, read delimited flat files, etc. It's not strictly tied to databases. And it's not just about inserting/retrieving. By its name, it models the data.. formats it in a standardized way.

[eluser]Banana Man[/eluser]
I would also suggest that when dealing with database driven sites, database design and model design are one of the most important tasks to deal with, in order to ensure a strong foundation is in place for the application.

[eluser]Colin Williams[/eluser]
Definitely, Banana Man. The first thing I usually do is map out my objects and how they relate. This, in turn, drives decisions about database design. I go that route just from working with other systems. It pays to have a solid, clear, thorough object model.

Thanks for the replies everyone, however, one thing is still not clear to me.

Is it "wrong" that a model would access the $_POST variable, or is there nothing explicitly wrong with that?

[quote author="callumd" date="1225952400"]Thanks for the replies everyone, however, one thing is still not clear to me.

Is it "wrong" that a model would access the $_POST variable, or is there nothing explicitly wrong with that?[/quote]

While technically it is possible to do, and at times might warrant it, I usually *pass* the post values to the models, for instance in a database update.

$update = array('col1' => $this->input->post('col1'));
$where = array('id' => '25');

$this->x_model->updateRecord($update, $where);

Then in my model I have:

function updateRecord($update, $where)
$this->db->update('table', $update, $where);

Hope this helps...

That helps, thank you.

I'm just trying to clear the lines in my head about what should go in the model vs what should go in the controller.

[eluser]Colin Williams[/eluser]
I also think models should not have to fetch data. They should always be supplied data by a controller.

Theme © iAndrew 2016 - Forum software by © MyBB