Welcome Guest, Not a member yet? Register   Sign In
Model, Controller, or View ?
#1

[eluser]skunkbad[/eluser]
I've got a database query in a model that returns all of the category names for products. That query array passes through a complex function to sort/organize it, and that function is currently in the model as well. Then, another function, which is a recursive function turns the array into a nested list based on category level. It would seem logical that the first function be moved to the controller, but since the second function is actually producing markup, I just don't know where it should officially live. It's not something I would expect to see in a view, but I've always thought that markup was to be applied in the view. What would you do?
#2

[eluser]developer10[/eluser]
[quote author="skunkbad" date="1260233354"]I've got a database query in a model that returns all of the category names for products. That query array passes through a complex function to sort/organize it, and that function is currently in the model as well. Then, another function, which is a recursive function turns the array into a nested list based on category level. It would seem logical that the first function be moved to the controller, but since the second function is actually producing markup, I just don't know where it should officially live. It's not something I would expect to see in a view, but I've always thought that markup was to be applied in the view. What would you do?[/quote]

in my opinion, there's no reason why you wouldnt call a function from the view..
#3

[eluser]BrianDHall[/eluser]
I'd have the function in the model, then have the view call the function through the model. The model owns the data, and is best to control manipulation of it into something a view could use, and I don't see a problem with a model function producing some markup if it is very data-specific, such as your use.
#4

[eluser]überfuzz[/eluser]
I always keep my html-rendering functions in a library. That way I get a good overview
Code:
$this->load->library('render_html');
$this->render_html->get_jadajada();
About the model and the sorting function. I like to get the info exactly the way I want it, in the controller. However, I'd say it's more a question of personal style. As long as you're sticking to one style I'd say it's cool.
#5

[eluser]jedd[/eluser]
[quote author="skunkbad" date="1260233354"]
Then, another function, which is a recursive function turns the array into a nested list based on category level.
[/quote]

FWIW, I'd have my model method produce a multi-dimensional array, rather than a nested list.

In your view, generate the nested list from the array - this would be fairly straightforward.

I'd be really wary about introducing any mark-up to data structures within the model.
#6

[eluser]summery[/eluser]
Turning a flat recordset into a meaningful nested data set (whether multidimensional array or something else) is a job for the model, IMHO.
#7

[eluser]skunkbad[/eluser]
It would seem, judging by the answers, that there really is no single correct way to do it. I will keep these answers in mind when I clean up what I've been working on, which is the categories menu for my Community Cart application. This is actually just part of a products display that I have been working on, and I've got a lot done. You'll be able to check it out soon. Thanks for your replies.
#8

[eluser]Sire[/eluser]
What about having the model pass the data to the controller, the controller function will loop through the data passing it to a view which contains a snippet of the markup. The resulting data will then be passed through to your main view. Something like this:

controller function
- get $category_data['categories'] from model

- loop through the data ...
foreach($category_data['categories'] as $category) :
$data['categories'] .= $this->load->view('category_list_view',$category_data,TRUE);
endforeach;

- $this->load->view('main_template',$data);

Your views contain all the markup this way, and your controller has all the code.
#9

[eluser]skunkbad[/eluser]
[quote author="Sire" date="1260266595"]What about having the model pass the data to the controller, the controller function will loop through the data passing it to a view which contains a snippet of the markup. The resulting data will then be passed through to your main view. Something like this:

controller function
- get $category_data['categories'] from model

- loop through the data ...
foreach($category_data['categories'] as $category) :
$data['categories'] .= $this->load->view('category_list_view',$category_data,TRUE);
endforeach;

- $this->load->view('main_template',$data);

Your views contain all the markup this way, and your controller has all the code.[/quote]

Some people will say that the markup, however small, doesn't belong in the view. I do think it matters, because if it didn't then there'd be no point in trying to follow the MVC pattern. Will somebody shoot at me for doing it my way? Probably.
#10

[eluser]Sire[/eluser]
Markup is the HTML, etc... and the View is exactly where it belongs. Did you mean markup no matter how small doesn't belong in the controller or model? If so, that's basically the same thing I illustrated in my example. I apologize if that wasn't clear or if my example sucks.




Theme © iAndrew 2016 - Forum software by © MyBB