CodeIgniter Forums
Passing values to model constructor - 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: Passing values to model constructor (/showthread.php?tid=7542)



Passing values to model constructor - El Forum - 04-14-2008

[eluser]MABUS[/eluser]
Hi guys,

I'm wondering how to be able to pass a variable or values to a model's constructor. Is that possible in ci? from the manual, when you call on to a model, the second attribute paramter that you pass to the function is the name that you pass when you want to use a different name for that particular model.

So is passing an argument to a model's constructor possible?


Passing values to model constructor - El Forum - 04-14-2008

[eluser]xwero[/eluser]
Could you give a scenario where it could be useful?


Passing values to model constructor - El Forum - 04-14-2008

[eluser]MABUS[/eluser]
Lets say I am loading a model, which extends to an abstract class. Now I'd like to pass something to the constructor, which is to be used only by the child class. Lets say the Parent class, which is abstract has more then one child. Lets say each chid is corresponding to a database entitiy, and each of those children has "profile" id in it, but of course they are of different types.

bottom line, what if I'd like to be able to pass something in a controller's constructor.


Passing values to model constructor - El Forum - 04-14-2008

[eluser]xwero[/eluser]
It isn't possible with the current CI code.

But i think what you are after can be solved with a set method in the abstract class.


Passing values to model constructor - El Forum - 04-14-2008

[eluser]wiredesignz[/eluser]
You could set a Controller class variable that the Model checks for and uses in its constructor. However while CI assigns all it's class variables to a Model, they may not be availabe until after the constructor is run. You'd need to check this.