CodeIgniter Forums
Question about the MVC pattern - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Question about the MVC pattern (/thread-9887.html)

Pages: 1 2 3


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]loathsome[/eluser]
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:
Code:
foreach($this->db_model->fetch() as $out):

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

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

Which method is the most "right" one?

I appreciate your input, guys!


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]Randy Casburn[/eluser]
The "most right" one is:

Quote: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. ;-)

Randy


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]loathsome[/eluser]
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


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]wiredesignz[/eluser]
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
Code:
-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



Question about the MVC pattern - El Forum - 07-11-2008

[eluser]Randy Casburn[/eluser]
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


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]loathsome[/eluser]
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:
Quote: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?

Quote: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.


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]wiredesignz[/eluser]
@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.
Code:
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();
    }
}



Question about the MVC pattern - El Forum - 07-11-2008

[eluser]loathsome[/eluser]
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!


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]wiredesignz[/eluser]
It's your choice really, I'm simply explaining another way to get the result to the view.


Question about the MVC pattern - El Forum - 07-11-2008

[eluser]loathsome[/eluser]
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