Part of the EllisLab Network
   
1 of 2
1
Question about the MVC pattern
Posted: 11 July 2008 01:05 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Hi CI people,

I have a question, regarding the mvc pattern. Is it “right” to OUTPUT content (store it in array) from a database from a model? Or should this be done first through a controller?

E. g, views/main.php have:

foreach($this->db_model->fetch() as $out): 

I’m thinking it really should be done this way instead:
views/main.php have

foreach($fetch_content as $out): 

And in controllers/main.php it fetches the content and passes it on

$data['fetch_content'$this->db_model->fetch();
$this->load->view('main'$data); 

Which method is the most “right” one?

I appreciate your input, guys!

Profile
 
 
Posted: 11 July 2008 06:33 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

The “most right” one is:

I’m thinking it really should be done this way instead:
views/main.php have
foreach($fetch_content as $out):

And in controllers/main.php it fetches the content and passes it on
$data[‘fetch_content’] = $this->db_model->fetch();
$this->load->view(‘main’, $data);

For sure.

Here’s one reference: http://c2.com/cgi/wiki?ModelViewController that says these guys invented MVC when they wrote about it here: http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html

So don’t take my word for it. wink

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 11 July 2008 10:05 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Thanks.

That’s what I thought to, but the small catch here is that if I do it that way, I’d have to fetch the content in every single controller. By just using the model in the view files, I avoid this.

smile

Profile
 
 
Posted: 11 July 2008 10:28 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

MVC design pattern states that a view may access the model directly, but it may not alter the model state.

So the call to model->fetch() should be implemented in the controller and if the resultset is stored in the model then the view can access it directly.

In fact CI actually creates a reference to the model for you to use in a view

-controller----------------------

$this->db_model->fetch(); //get the data

$this->load->view('main');

-
view----------------------------

foreach(
$this->db_model->resultset as $out): //get the result from the model 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 11 July 2008 11:40 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Hey wired, I don’t think that addressed his statement.  He specifically made reference to “just using the model in the view files”.  I think he is implying that he wants to “bypass” the controller and instantiate the Model from within the View.  This would break the Pattern as you’ve so aptly described it as an instantiation of the Model by the View would definitely be “altering the model state”.

@loathsome—Welcome the MVC.  Some thought is required for any design “before” the code goes in the machine.  Paper is good.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 11 July 2008 11:41 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Hi, and thanks for your reply.

I am autoloading my model now, and accessing it directly in the view file. I find this a lot easier than initializing the model in EVERY single controller.

I’m nut sure what you mean about this though:

MVC design pattern states that a view may access the model directly, but it may not alter the model state.

What is a model state?

So the call to model->fetch() should be implemented in the controller and if the resultset is stored in the model then the view can access it directly.

What is a resultset, and how can this be stored in a model? Is my fetchin function in the model storing anything?

I appreciate your replies very much. I’m a total newbie to this MVC pattern, but I am not indenting to give up until I have a 100% control over it.

Profile
 
 
Posted: 12 July 2008 12:24 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

@loathsome, calling fetch() from the view is altering the model state inappropriately.

The resultset is the query result from your db->fetch() call. If you create a class variable in the model you can store the result to that variable.

class Db_model extends Model
{
    
var $resultset = array();

    function 
Db_model()
    
{
        parent
::Model();
    
}

    
function fetch()
    
{
        $query 
$this->db->get('table');
        
$this->resultset $query->result();
        return 
$query->num_rows();
    
}
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 12:42 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Wait, what? When should I call the fetch() function then? From a controller? Because that will be, as stated numerous times, a heck of a lot more work.

Thank you!

Profile
 
 
Posted: 12 July 2008 12:58 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

It’s your choice really, I’m simply explaining another way to get the result to the view.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 12:59 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

So your personal recommendation would be to fetch the content in every single controller instead of doing it the way I’m doing it now? smile

Profile
 
 
Posted: 12 July 2008 01:02 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

It does seem to break the rules doing it that way… but if no-one is looking, who cares. LOL

CodeIgniter provides access to the model inside the view, So I guess there are no REAL rules here.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 01:05 AM   [ Ignore ]   [ # 11 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

He likely needs help thinking through his design rather than his fetch() statement.  Do you think?  If he’s got 500 controllers there might be a different kind of issue.  If he really, really needs 500 controllers he needs to write a Controller Interface and get it done once.  But these are design issues, not fetch() issues.

IMHO,

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 12 July 2008 01:17 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

The application have, as per today, about 10 controllers, and right now every single one of them looks like this:

class VerifyId extends Controller {

function index()
{
   $data[
'menu'$this->menu_model->fetch();
   
$this->load->view('verified/verifyid'$data);
}
function someotherFunction()
{
   $data[
'menu'$this->menu_model->fetch();
   
$this->load->view('etc/someotherFunction'$data);
}
  
// et cetera, et cetera

It’d be so much simpler to just FORGET about fetching the menu, and let the model do it’s job directly from the view file. Don’t you agree?

How does other applications do it, when having for example a sidebar? I really fail to see how my method is “wrong”, seeing as it is much, much easier to apply. Still, I want to do this the right way.

@Randy Casburn: What do you mean by a «controller interface»? Many thanks.

Profile
 
 
Posted: 12 July 2008 01:22 AM   [ Ignore ]   [ # 13 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

I agree with Randy. It’s a design issue, not an MVC issue.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 12 July 2008 02:40 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

So what do you recommend I do, then?

Profile
 
 
Posted: 12 July 2008 04:17 AM   [ Ignore ]   [ # 15 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

What exactly are you doing that makes you think you need fetch the results from the view? We’re not in tune with your app, so we can’t offer much more advice.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
   
1 of 2
1