Welcome Guest, Not a member yet? Register   Sign In
How do you pass initial parameters to a model that is being loaded?
#1

[eluser]CoderReborn[/eluser]
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?

Thanks.
#2

[eluser]WanWizard[/eluser]
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.
#3

[eluser]CoderReborn[/eluser]
How do I do that? I'm new to CI.
#4

[eluser]WanWizard[/eluser]
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.
#5

[eluser]n0xie[/eluser]
Or alternatively, you can create a function init() which accepts an array of parameters and call that explicitly whenever you load the model.
#6

[eluser]WanWizard[/eluser]
Probably a lot easier then extending the loader class, for someone new to CI.




Theme © iAndrew 2016 - Forum software by © MyBB