Welcome Guest, Not a member yet? Register   Sign In
Running multiple models from a single controller
#1

[eluser]JoostV[/eluser]
I really like what I've seen from CI and would like to use it! But, being a total newbie to CI I thought I might ask. What is the most efficient way to implement multiple models from a single controller?

In a non-CI way of programming I call different models from my controller index.php. Most of them are the same for every http request, so I hard code them into the controller. One of them is dynamically retrieved from the URI. Typically, I end up with something like this:
Code:
$main = new main(); // Load all the core classes, open database connection, etc.

$navbar = new navbar();
$data['navbar'] = $navbar->getData(); // Retrieve links for navbar from DB and put them into $data array

$recentNews = new recentNews();
$data['recentNews'] = $recentNews->getData(); // Retrieve recent newsfrom DB and put it into $data array

$content = new $uri[0]; //Dynamically call a class, based on URI, e.g. /articles/1-1. This class will retrieve appropriate content from DB and put it into $data array

$view = new view(); //Instantiate view class
$view->display($data); //Call template and pass all the acquired data into it

How would you go about doing a thing like this in CI? Build a single controller in application/controllers and load different models from application/models?
#2

[eluser]Craig A Rodway[/eluser]
Have a look at theses User Guide pages to get a feel of how Code Igniter handles: controllers and models.
#3

[eluser]xwero[/eluser]
Code:
class Someclass extends Controller
{
   function Someclass()
   {
     parent::Controller();
     $this->load->database();
     $this->load->model('Somemodel','',true); // model used by several methods
   }

   function somepage()
   {
      $this->load->model('Somemodel2','',true); // model only used in this method
   }
}
#4

[eluser]JoostV[/eluser]
Thanx guys,

I guess I read through the models section in the user manual too hastily. Sorry about that.

Makes a lot of sense, this approach.




Theme © iAndrew 2016 - Forum software by © MyBB