Welcome Guest, Not a member yet? Register   Sign In
model inclusion from a view
#1

[eluser]stratosg[/eluser]
hi there guys!

i am really really new to codeigniter and i trully love it! i have started a project on it both as a job and as a good way to know the framework better. so here is my problem. i have a view that i want to bring some stuff in. i need a model to do so. but, although i include it, i get the error "Message: Undefined property: CI_Loader::$menu_loader". it's as if it's not loaded but when i print_r $this i can see it there. my question is can i use a model from a view or this is not possible? and if not do you have a suggestion?
#2

[eluser]dmorin[/eluser]
A couple of questions. First can you post samples of all relevant code? So post your controller, maybe part of the model, and the part of the view that gets the info?

Second, are you loading the model from the controller? If not, it's considered a best practice. Think of your controller function as being a traffic director/mail carrier/(more metaphors here). The way I do it is I load the model and request the information from the controller. Then I load the view from the controller and pass in an array of data including the model information. Make sense?
#3

[eluser]stratosg[/eluser]
Here is the thing. I used the tips from here http://ellislab.com/forums/viewthread/52211/ to implement the header/footer thing. So, i need the header and the footer to load the menus from the db when they are rendered. I would like to avoid doing so from the controller for one reason. I would have to do so in every controller i make. So, as i see it, i have to call it from every controller. What i am asking is if the view can load a module and run it. I know the better practice is to do so from the controller bu as you can see it's not that practical here. Maybe you have any other suggestion?

As for my code i have a main view file index.php (as the tread i said suggested) that has these inside:

Code:
<?php
//master view - index.php
$this->load->view('header');
$this->load->view($page);
$this->load->view('footer');
?>

Then, from header i have these that are in interest:

Code:
<div class="sidebar_left">
    &lt;?php
        $this->load->model('menu_loader');
        $this->menu_loader->load_menus('header');
    ?&gt;
</div>

Now, i know the loading goes well since if i try print_r($this) i can see the module there. But the load_menus call fails and the error is:

Quote:Message: Undefined property: CI_Loader::$menu_loader

Then php fails completely with this error:

Quote:Fatal error: Call to a member function load_menus() on a non-object

This happens on the same line... Any suggestions?
#4

[eluser]dmorin[/eluser]
I'm not sure the best solution, but to start, if you're going to use this on every page anyway, just autoload the model using the autoload config file. Then see if you can make that model call.
#5

[eluser]simonmaddox[/eluser]
You should be doing this inside your controller. Models and Views should only interact with the controller - that's what passes data between the two.
#6

[eluser]stratosg[/eluser]
@dmorin

i added it to the autoload but the call still fails.

@simonmaddox

so you are saying that my only option is to do so from every controller? sorry for asking again but since i have no experience with the framework i am wondering if there is an alternative...

thanks for your replies guys...
#7

[eluser]dmorin[/eluser]
Is the error message different now that it's auto loading?
#8

[eluser]stratosg[/eluser]
nope... autoloading, loading from the controller or loading from the view produces the same error when i try to call the model's function from the view...
#9

[eluser]dmorin[/eluser]
From a normal view used via $this->load->view() you should be able to call a method in a model. However, if you're loading it differently, you may have to use the library trick and do the following:

Code:
$CI =& get_instance();
$CI->menu_loader->load_menus('header');
#10

[eluser]stratosg[/eluser]
this one did work although i load the view the normal way with $this->load->view()

anyway thanks for all the help guys! i really appreciate it...




Theme © iAndrew 2016 - Forum software by © MyBB