CodeIgniter Forums

Full Version: Is this an OK MVC implementation?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Leon Stafford[/eluser]

Is this an OK way to use a MODEL?

I'm trying to get my controllers as lean as possible and get as much design into views to allow the designers to do anything visual (and keep me happy in the code ;-P ).

I have an event list which is pulled from the DB. In my fat CONTROLLER I'm just saying:

//load Eventlist Model (with autoconnect to DB specified)
        $this->load->model('Eventlist', '', TRUE);
        //get eventlist for top page
        $event_list = $this->Eventlist->get_top_event_list();

Then passing it to my main VIEW like:

$array_to_pass['calendar'] = $calendar;
        $array_to_pass['event_list'] = $event_list;
        $view_data = array('content' => $array_to_pass);
        $this->load->view('tsukashin_index', $view_data);

In my MODEL, I grab data from the db, then return a MODEL-specific VIEW's output back to the CONTROLLER using:

/**list all upcoming events
        *This takes each array of results, passes them to the
        *VIEW and returns the combined output in a variable to be
        *used in the controller where it is passed to the top page's
        foreach ($this->upcoming_events as $row){
            $view_data = array('row' => $row);
            $event_list.= $this->load->view('event_list_index_view',$view_data,TRUE);
        return $event_list;

My VIEW for that loop just looks like this:

<div id="upcoming_event">
    <a href="view/event/&lt;?=$row['year'];?&gt;/&lt;?=$row['month'];?&gt;/&lt;?=$row['day'];?&gt;/">&lt;?=$row['year'];?&gt;.&lt;?=$row['month'];?&gt;.&lt;?=$row['day'];?&gt;</a>
    <br />
    <a href="view/event/&lt;?=$row['year'];?&gt;/&lt;?=$row['month'];?&gt;/&lt;?=$row['day'];?&gt;/&lt;?=$row['id'];?&gt;/">&lt;?=$row['event_name'];?&gt;</a>
    <br />
    &lt;!-- limit the event_text to certain amount of characters --&gt;
    &lt;?php $event_excerpt = mb_substr($row['event_text'], 0, 50,'UTF-8'); ?&gt;
    <p>&lt;?=$event_excerpt;?&gt; - <a href="#">続きを読む</a></p>


For me, it's already a lot cleaner than when I had most of it stuck in the top level VIEW and big bits in the CONTROLLER.

I'm just wondering if this is an ideal way to use MVC or is it too nested?

Any opinions appreciated!



El Forum

[eluser]Colin Williams[/eluser]
Model's shouldn't load views. They should just handle data operations.