Welcome Guest, Not a member yet? Register   Sign In
Controller / model relationship, handling query errors, multiple queries per model method
#1

[eluser]batfastad[/eluser]
Hi everyone

I'm new to CI and have quickly learnt that there are now many different ways to do things! So the questions below are more about what I should adopt going forward.

1) I often have to run multiple queries that depend on each other, so an insert in one table, getting the resulting ID and inserting another record in another table, and so on.
Is it best to separate these into their own model methods?
Would this be a good way to approach it... if both queries will only ever be run together, sequentially, then have them in the same model method.
If one of the queries may be run on its own, then should I have them as separate methods?

2) Where should I do my data validation and pre-processing?
At the moment I'm doing this in my controller and passing my finished data as an array to my model method, because depending on the results of the validation I might not even need to call my model.
But I have heard that some people like to keep their controllers as light as possible, which would infer that they have the validation logic in the model.

3) Checking model/db success and return.
In my model, is it ok to do something like this...
Code:
$q = $this->db->query($query);
if ($q->num_rows() > 0) {
return $q->result_array();
} else {
// NO RESULTS
return FALSE;
}
Then to test the output of that, in my controller...
Code:
if ($data = $this->my_model_name->bookingslist()) {
$this->viewdata['bookings'] = $data;
}

// LOAD VIEW
$this->load->view('bookings_index', $this->viewdata)
So I'm basically testing for data or false in the model and then again in the controller.
Can I dispense with the test in the model?
Is there a recommended way of doing this I should adopt?

4) Templating. I've adopted an unofficial templating method.
What I wanted to achieve was a way of making sure that all front-end code for a particular page was stored in a single view file.
So in my view I don't actually echo out the page. I set the HTML up in variables (using heredoc blocks). This allows the actual HTML code to be clear of any inline PHP which makes it cleaner if a designer has little PHP knowledge.
Then after that, at the bottom of my view, I require('template.php') which is another view file, but this does the actual echoing of the output.
This really works for me.
The templating libraries I looked at involved having to load multiple views... loading the first into a variable with the second view being the template with the variable being passed in. I found I had to set any page-specific stuff that I wanted in a separate block (e.g. any JS/style to appear in the head of the template rather than the body) had to be stored in the controller. I much prefer having ALL the HTML code needed for a page stored in a view. Plus I can do this without needing an additional library.
Is this require()ing another template view from within my view inherently wrong?

Cheers, B




Theme © iAndrew 2016 - Forum software by © MyBB