Welcome Guest, Not a member yet? Register   Sign In
Question about the MVC pattern
#1

[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!
#2

[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
#3

[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
#4

[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
#5

[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
#6

[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.
#7

[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();
    }
}
#8

[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!
#9

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB