Welcome Guest, Not a member yet? Register   Sign In
how's models loaded should be in MVC pattern?
#1

[eluser]metaltapimenye[/eluser]
soory for my english..

when i got latest version of CI (1.7.1), i realize there's something change with models load protocol even it might be happen long on older versions. when i do this in my model, some coding style meet its limitation..

Code:
//location:/application/models/one.php

//retrieve protocol:$this->one->get_something();

class one extends Model {
    function one()
    {
        parent::Model();
    #$this->load->model('two'); # if this line uncommented, everything works just fine
    }

    function get_something(){
        $this->load->model('two'); # <-- getting other models, will result 'two' object not identified. kind of bush in the throat uh?
    return $this->two->get_method_two(); #<-- generating error
    }
}
yes, & get_instance() stuff will make it work. but hey, it just models->models layer transaction, why should dig too deep?

for short term project it would not be a problem, but if we working a huge and long term project .. it might make some models wont work after upgrade, and yes, it could be a pain in the ass to fix it.

yesterday, i make some tryouts on CI 1.7.1
Controller:
Code:
class Blog extends Controller {

   function Blog(){
        parent::Controller();    
   }
   function index(){
        $this->load->view('view_page');
   }
}

View:
Code:
&lt;!--html stuff--&gt;
&lt;?php
$this->load->model('blog_model','blog');

echo $this->blog->header();
echo $this->blog->content();
?&gt;
<br>
&lt;?php    $this->load->view('other');
echo $this->blog->footer();

?&gt;

...
and work as a charm.

it would be a pleasure for an old cowboy like me to applying this coding style. But i reminded by the way loading model->model and i might pissed when i upgrade which is not allow me to do some stuff like this way.

in best practice, how's models loaded should be in MVC pattern?
#2

[eluser]Phil Sturgeon[/eluser]
Why are you using Models to output headers and footers? Models should not return HTML but should fetch and manipulate data from database, XML, REST, etc.

HTML should go into views.

If you wish to load one model from another, do this:

Code:
class one extends Model {
    function one()
    {
        parent::Model();
    #$this->load->model('two'); # if this line uncommented, everything works just fine
    }

    function get_something(){
        $CI =& get_instance();
        $CI->load->model('two');
        return $CI->two->get_method_two(); #<-- generating error
    }
}
#3

[eluser]BrianDHall[/eluser]
Yes, Phil is right on all counts, but I figure I will add a little extra info to let you know why yours isn't working.

In a Model $this refers to the object Model, not to the main CI object that $this refers to in Controllers and Views. This drove me nuts when I was trying to update sessions from a Model for authentication purposes, and Phil's solution is the right one.




Theme © iAndrew 2016 - Forum software by © MyBB