Model calls from view?

#1
[eluser]erik.brannstrom[/eluser]
Hi!

I'm a bit curious how hardcore-MVC members of the CI community are. Something that comes up every now and then when making some type of database based application is that I fetch some rows from one table and then I need to make queries using data from the first query.

An example from the site I'm building right now: When making a search for a user a resultset with all users matching the search is returned, and while I'm looping through the results in my view I fetch that user's three most popular artists (it's a site for record collectors).

This is obviously a MVC faux pas, making database calls from the view, however it seems like a waste of resources to foreach through the results both in the controller and in the view.

One solution is obviously to make a custom function in the controller that returns a string that can be displayed in the view. I'm wondering how other people do this? Perhaps looping twice isn't such a waste at all?

Best regards,
Erik Brännström

#2
[eluser]Randy Casburn[/eluser]
Hi Erik,

First, I'm not a zealot when it comes to MVC, okay?

Your approach is perfect. But this statement is true only if several preconditions are met. First, if you only ever build one application. Two, if you never want to change your "all users matching" result set. Three, if you never ever use the "top three" result set again.

So may I ask you to shift your thinking a little bit? Beyond one application and one "page" view.

The idea behind any design pattern is not rigidity, but making our software more robust and easier to modify...and reuse.

So the bottom line? If you don't ever want to stop rewriting the same stuff over and over and over...your design is perfect. Otherwise, listen to some of the smart folks here like wiredesignz, xwero, the dereks, and many others. Soon, you'll simply be reusing huge parts of controllers and modules and customizing templates for the views. Then what "makes sense" will have a different context.

Just a thought...but one I hope is helpful,

Randy

#3
[eluser]Chris Newton[/eluser]
I'd tend to do a 'join' when pulling the userdata the first time. I try not to create too many specific individual database calls in the model, but I'd prefer to do something like this in the controller

$user_array=$this->my_model->get_users_and_top_three_favorites($user_id);

And make that method in the model pull the top three & the user data at the same time. OR, I'd do a call to the model in the controller and then during a foreach, make an additional call to the model for the faves.

I wouldn't do this in the view, ever, because there's no point in building a view if you're also going to make it do work. Just echo everything out from the contoller. I think it's appropriate to directly call the model from the view if you're simply pulling data without acting on it. When you're "viewing" it. I don't do that myself though, I'd rather assemble the necessary data in the controller and then simply display it in the view.

After having done this a while, I've started to really try to refine my approach so that I really can reuse controllers & views. It's a learning process, but my code's getting better because of it.

#4
[eluser]Alex.[/eluser]
I've been thinking about this recently, and I've come to the conclusion that

a) it's more code in the view, and I'd like to keep my views clutter free
b) As mahuti said, it makes the view do work, which isn't a good idea

One way of getting around the problem is to generate the data in the controller, then cache it using a library such as KHCache.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.