Welcome Guest, Not a member yet? Register   Sign In
Load model with a constructer - possible?
#2

[eluser]phpoet[/eluser]
I have the exact same question. In particular, many of my models load data from a row in a database table. It would be cool if I could pass in the id of the database table row when loading the model in the controller.

For example, it would be neat if you could write something like this.

Code:
$this->load->model("Product", $product_id);

Of course this would not work as the 2nd parameter is an alias name for the model you are loading.

I have considered creating my own Loader class by extending the existing Loader class in CI then modifying the model() function (in system/libraries/Loader.php) so that it takes an array as an optional 4th parameter that could be exploded into parameters when loading the model. Then you could at least write something like:

Code:
$this->load->model("Product", '', '', array('id'=>12));

What I'd probably do is make that array the 2nd parameter since it's rare that I provide an alternative name. I also autoload my db connection in almost all my apps. Then in my new Loader class's model function I'd check to see if the 2nd parameter was an array or an integer. If it was an integer I would assume that the number was the id (primary key) in the database table for the row that should be loaded. If it was an array I'd explode the array into the parameters for the model's constructor.

I'm very interested in other people's thoughts on this topic as well. Thanks!


Messages In This Thread
Load model with a constructer - possible? - by El Forum - 08-26-2008, 02:42 PM
Load model with a constructer - possible? - by El Forum - 08-27-2008, 01:58 PM
Load model with a constructer - possible? - by El Forum - 08-27-2008, 02:27 PM
Load model with a constructer - possible? - by El Forum - 08-27-2008, 03:08 PM
Load model with a constructer - possible? - by El Forum - 08-27-2008, 11:05 PM
Load model with a constructer - possible? - by El Forum - 08-28-2008, 08:08 AM
Load model with a constructer - possible? - by El Forum - 08-28-2008, 09:58 AM
Load model with a constructer - possible? - by El Forum - 08-28-2008, 12:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB