• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you pass initial parameters to a model that is being loaded?

I have a model called "question" which takes 4 required parameters in the constructor.

The problem is that when I try to load this model from my controller, I get errors because
the "question" constructor is expecting the 4 parameters.

$this->load->model('question') ; /* How do I pass the 4 parameters to the "question" constructor? */

Is "loading" the same thing as instantiating a new object with the PHP "new" keyword?


You have to extend the Loader library, and then update the model() method. And this does a lot more than 'new', so I guess the answer to that is no.

How do I do that? I'm new to CI.

See the manual, "Creating Core Classes", "Extending Core Class".

Copy the model() method from ./system/libraries/Loader.php to ./system/application/libraries/MY_Loader.php, and modify it to your liking.

Or alternatively, you can create a function init() which accepts an array of parameters and call that explicitly whenever you load the model.

Probably a lot easier then extending the loader class, for someone new to CI.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.