EllisLab text mark
Advanced Search
1 of 2
1
   
Question about the MVC pattern
Posted: 11 July 2008 02:05 PM
Avatar
Joined: 2008-07-10
77 posts

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!

 
Posted: 11 July 2008 07:33 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-06-19
1002 posts

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

 
Posted: 11 July 2008 11:05 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

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

 
Posted: 11 July 2008 11:28 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts

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

 
Posted: 12 July 2008 12:40 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-06-19
1002 posts

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

 
Posted: 12 July 2008 12:41 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

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.

 
Posted: 12 July 2008 01:24 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts

@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

 
Posted: 12 July 2008 01:42 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

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!

 
Posted: 12 July 2008 01:58 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts

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

 
Posted: 12 July 2008 01:59 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

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

 
Posted: 12 July 2008 02:02 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2937 posts

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

 
Posted: 12 July 2008 02:05 AM   [ # 11 ]   [ Rating: 0 ]
Joined: 2007-06-19
1002 posts

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

 
Posted: 12 July 2008 02:17 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

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.

 
Posted: 12 July 2008 02:22 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

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).

 
Posted: 12 July 2008 03:40 AM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-10
77 posts

So what do you recommend I do, then?

 
Posted: 12 July 2008 05:17 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

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).

 
1 of 2
1