CodeIgniter Forums
How do you pass initial parameters to a model that is being loaded? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How do you pass initial parameters to a model that is being loaded? (/showthread.php?tid=30663)



How do you pass initial parameters to a model that is being loaded? - El Forum - 05-21-2010

[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.


How do you pass initial parameters to a model that is being loaded? - El Forum - 05-21-2010

[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.


How do you pass initial parameters to a model that is being loaded? - El Forum - 05-21-2010

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


How do you pass initial parameters to a model that is being loaded? - El Forum - 05-22-2010

[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.


How do you pass initial parameters to a model that is being loaded? - El Forum - 05-22-2010

[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.


How do you pass initial parameters to a model that is being loaded? - El Forum - 05-22-2010

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