Welcome Guest, Not a member yet? Register   Sign In
Controller / model separation, templating
#1

[eluser]batfastad[/eluser]
Hi everyone

Just getting started with CI and currently converting a simple project to MVC. I have a few questions on what the best practice is with models and controllers.

1) Validation of form data, should I do this in the controller or in the model? My guess is controller, as depending on the result of the validation I might not even need to run DB queries, e.g.: if required data is missing.

2) If I have a controller function that needs multiple but separate DB queries, should I set each query up as a seperate function in my model?
In this case the queries are separate, they will only ever be run one after the other, never individually.
Or should I combine the queries into a single function in my model?

3) I'm defining a constructor in each model...
Code:
public function __construct() {
$this->load->database();
}
Even if I'm calling multiple model functions from the same controller function, then there's still only one instantiation of the class so there should only be a single DB connection. Is that right?

4) But to be on the safe side should I just set the database to auto-load in my config?
I don't really want multiple DB connections going off when they're not really needed.

5) I'm setting up my forms manually at the moment, not using the helpers yet.
I have a form that sends data via a jQuery AJAX POST and the result that's sent back is a line of HTML, either success or error. Should I still have that coming out of a view or should I echo that out directly from the model or controller?
It seems a bit pointless setting up a view just to display a simple success/error message.

Sorry if all these are obvious questions. I just wanted to get some definitive opinions before carrying on.

Cheers, B
#2

[eluser]batfastad[/eluser]
Oh, ignore the title bit about templating. That's for another question.
#3

[eluser]ojcarga[/eluser]
Hi

1) In the controller.
2) This is up to you and the app workflow, in the case you explain above maybe would be better for you to user just one method.
3) I am not sure if I really understand this point Sad
4) If all your app use the db, YES, if no it is not necessary.
5) I would say that if you sent back HTML you should use views to hold that HTML, this way you are going to have a cleaner and separated code.

BTW: the correct name for functions that are inside a class is method, not function Wink
#4

[eluser]batfastad[/eluser]
Thanks for the reply. That's helped alot.

I'll try re-explaining #3.
I'm assuming that each time I run
Code:
$this->load->database();
a DB connection is created, as I have autoinit set to TRUE in my config

I have a constructor in each model which runs the above code and creates the connection.
Does this constructor only get fired when I call the first method in a model?
I assume the constructor won't fire when I make subsequent calls to methods in the model as the model class has already been constructed?

And a further question:
6) Do I use a view for all output, e.g.: if I'm outputting JSON, XML or an ical/vcard?
Or are views just for HTML?

Cheers, B
#5

[eluser]dejan[/eluser]
[quote author="batfastad" date="1348296815"]
Code:
$this->load->database();
a DB connection is created, as I have autoinit set to TRUE in my config
[/quote]

You don't have to load database from the constructor all the time. You can just add it to application/config/autoload.php. Skip to the section "Auto-load Libraries" and make it look something like: $autoload['libraries'] = array('database');

[quote author="batfastad" date="1348296815"]
I have a constructor in each model which runs the above code and creates the connection.
Does this constructor only get fired when I call the first method in a model?
[/quote]

The constructor is called only when you first CREATE the object. It's not called ever again (and certainly not before method calls). E.g. take this code for an example:

Code:
class MyObject {
  public function __constructor($param) {
    echo "Constructor called with parameter $param";
  }

  public function method() {
    echo "Some method";
  }
}

$obj = new MyObject("foobar"); // This echos "Constructor called with parameter foobar". You can see the constructor was called, and that's it.
$obj->method(); // This echos "Some method". Constructor is never called again.

[quote author="batfastad" date="1348296815"]
6) Do I use a view for all output, e.g.: if I'm outputting JSON, XML or an ical/vcard?
Or are views just for HTML?
[/quote]

As you wish, really. For JSON, it doesn't make sense to call the view, because you can always do something like echo json_encode($stuff); and be done with it. You can do it in the view as well, but as it will usually end with that one line, no need for a view.

I personally only use views for HTML. However, there are people who prefer to create a view for e.g. XML and do something like

Code:
<element>&lt;?php echo $value; ?&gt;</element>

I prefer to construct XML using PHP DOM so I do everything in controllers and then just echo the output.
#6

[eluser]batfastad[/eluser]
Ok this is great stuff so far.

Final questions for now...
7) Is it valid to return FALSE from a method in a model and test that in the controller?
For example...
Code:
// CONTROLLER
if (!$data = $this->model->query($input)) {
    show_error('Model returned false');
} else {
    // LOAD VIEW
}

8) Where do I post-process data from my model, e.g. date formatting, number formatting any other if() blocks to build up full HTML strings etc.
I'm guessing this should happen in the view since it's output/display related. Is that correct?

9) I'm really not keen on using inline PHP. Especially when looping through data and building multiple tables and complex pages as it makes the design of the view more difficult to update.
Is it acceptable to do all my looping, processing etc at the top of the view in PHP, then echo a heredoc block containing the HTML code with PHP vars?

Cheers, B
#7

[eluser]dejan[/eluser]
[quote author="batfastad" date="1348359839"]Ok this is great stuff so far.
7) Is it valid to return FALSE from a method in a model and test that in the controller?
For example...
[/quote]

Sure it is, why wouldn't it be?

Quote:8) Where do I post-process data from my model, e.g. date formatting, number formatting any other if() blocks to build up full HTML strings etc.
I'm guessing this should happen in the view since it's output/display related. Is that correct?

I prefer to do all formatting in the view.

Quote:9) I'm really not keen on using inline PHP. Especially when looping through data and building multiple tables and complex pages as it makes the design of the view more difficult to update.
Is it acceptable to do all my looping, processing etc at the top of the view in PHP, then echo a heredoc block containing the HTML code with PHP vars?

No, don't do it like that. If you do, you'll end up with a mess like Wordpress Smile Take a look at this: PHP: Alternative syntax

That syntax is devised for control structures (loops, ifs, etc.) when outputting HTML. So, e.g. in your view, you would do something like:

Code:
<h1>Products</h1>
&lt;?php foreach ($products as $product): ?&gt;
<h2>&lt;?php echo $product->name; ?&gt; <span>&lt;?php echo $product->price; ?&gt;</span></h2>
Description: &lt;?php echo $product->description; ?&gt;
Last updated: &lt;?php echo date('m/d/Y', strtotime($product->date)); ?&gt;
&lt;?php endforeach; ?&gt;

That looks very clean, in my opinion, and is easy to read and maintain.




Theme © iAndrew 2016 - Forum software by © MyBB