Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Something that has always bothered me is doing more than one loop to manipulate an array.

What I mean is, in the controller the data is fetched from the DB via a model. Lets say we are showing a list of users, and each user has a status (1,2,3 equates to verified, unverified, banned respectively). Within each iteration of the loop the status would be checked and displayed via another Db query (forget mysql joins in this example).

Now, would you do that in the controller within a loop, and then perform another loop in the view with all the data already fetched and pre-formed ready for display (therefore resulting in 2 loops).

--OR--

Would you just do it in the view therefore resulting in the one loop but with model calls from the view. I understand that this is ok in the strict MVC pattern but its frowned upon generally.

It seems silly to loop twice but then its tidier as all the data manipulation is kept within the controller.

share|improve this question
    
Thanks all for your answers. I understand the ideal answer here is to use the DB to handle this kind of data request. Perhaps I can rethink what Im doing. At the moment Im just trying to plan porting my code into an MVC pattern for future scaling. To make things clear when I talk about users, Im actually just talking about a list of email adddresses essentially. Its nothing to do with ACL in this context. Should have made that clearer. –  LeeRM Sep 21 '11 at 22:33

3 Answers 3

up vote 2 down vote accepted

I would do that nor in the view or the controller but on the model.

I explain :

  • Your controller's job is to retrieve the expected user list, check ACL, etc...
  • Your view's job is to present this data in an elegant form
  • Your Model job's in to fetch/store data from Database and ensure integrity. Userstatus is a model too for me.

My configuration make this pretty easy, I use mustache (Php port) for view, which allow me to call methods from my models directly in view. I wrote my own ORM for my models, that way I have wrappers.

Such code would look like that for me :

// Controller
$template = new Template('pages/users.html');
$template->users = mUser::find(); // return array of mUsers instances
echo $template->render();

// View
{{#users}} <!-- For each user -->
  {{getName}} has status {{#getStatus}}{{getStatusName}}{{/getStatus}}<br />
  <!-- getStatus is a method from mUser model, that return a mUserStatus instance -->
{{/users}}

/* More explain on the view syntax
{{name}} = $user->getName() (return string)
{{getStatus}} = $user->getStatus() (return instance of mUserStatus);
{{statusName}} = $user->getStatus()->getStatusName();
*/

You may want to have request caching for each model instances in request level so that you never runs a request twice times if not needed.

That seems more natural to me than to delegate it to controller. I try to put business intelligence on controllers, there is no need for intelligence nor programmer intervention to retrieve a status name for each user.

I Hope it help.

share|improve this answer
    
-1 for I try to put business intelligence on controllers. But your code looks correct. –  OZ_ Sep 21 '11 at 12:15
    
Can you explain your point of view ? –  Sébastien VINCENT Sep 21 '11 at 12:16
    
Models is a place for logic. Controller it's just a tool to pass request from user to Model and return answer to user (presented by View usually). –  OZ_ Sep 21 '11 at 12:18
2  
I does not agree. Models should take care of data storage and integrity. What i call business logic is : authentication, authorization, Choosing the view and send parameters to it, maintaining application state, handle errors. Controllers must control the workflow. Models are in no way a place to be for all that. What kind of logic do you have in your models ? –  Sébastien VINCENT Sep 21 '11 at 12:40
3  
what about skinny controller, fat model? Wouldnt the model handle some of this logic? Ive read so many conflicting opinions on this subject - I think its just that - an opinion. Do whatever suits within the guidelines. eg. littlehart.net/atthekeyboard/2007/04/27/… –  LeeRM Sep 21 '11 at 22:34

In my opinion logic that manipulates the data you're returning, should be located in the controller. Logic that manipulates the representation of your data can be located in the view. So I would go for the second option.

But, as you pointed out yourself this is a choice of implementation.

Also note that multiple round trips to your DB are bad for performance. Your example is a typical n+1 problem, meaning that you have 1 'top' select query and then N more queries for each row in your first result set. If you encounter such a problem always try to solve them on the DB level.

Another note I would like to add is that in your example you're storing status explanations in the DB. If you want to provide your applications in other languages, this might prove to be a problem. But this is beyond the scope of your question :)

share|improve this answer

Doing two loops is the clean way. That is what I would do for most cases, but I think there is no gerneral answer to this. Like if you have a lot a data and performance gets an issue it would be better to forgett about MVC and just use one loop.

A third way would be to use a helper function you can call from the view. Now that I think about it... that would probably be the best way.

share|improve this answer
    
"Helpers" is the code which has been missed in models. –  OZ_ Sep 21 '11 at 12:14
    
I am talking about Zend View Helpers –  PiTheNumber Sep 21 '11 at 12:29
    
it would be better to forgett about MVC - lol –  OZ_ Sep 21 '11 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.