CodeIgniter Forums
Problem between using a Model or using a Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Problem between using a Model or using a Library (/showthread.php?tid=5719)

Pages: 1 2


Problem between using a Model or using a Library - El Forum - 02-01-2008

[eluser]diez[/eluser]
What i want to do:

i want to be able to create multiple instances of a class (model or library) but as well be able to pass custom parameters (or an array of variables) to the model or library while using CI's framework.

My problem:

I seems like i can create multiple instances of a using a model, as such:
Code:
$this->load->model('Model_name', 'cat');
$this->load->model('Model_name', 'dog');
$this->load->model('Model_name', 'rat');

$this->cat->function();
$this->dog->function();
$this->rat->function();

now, i want to be able pass in a third parameter, as such:
Code:
$arr_cat_specs = array('height'=>'1 foot', 'weight'=>'5lbs', 'colour'=>'white');

$this->load->model('Model_name', 'cat', $arr_cat_specs);
however, in the model documentation is says the third parameter is used to configure database settings.

Now if i were to use a library class, i can submit parameters with custom data, as such
Code:
$params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params);
however i can't seem to create multiple instances like i can with a model.


Problem between using a Model or using a Library - El Forum - 02-01-2008

[eluser]BizComputing[/eluser]
Is there a specific reason you need to pass the data as part of the model load? I would think the easiest solution would be to put an init method in your model, and after load, call your init method with all the arguments you could possibly need.


Problem between using a Model or using a Library - El Forum - 02-01-2008

[eluser]diez[/eluser]
ya, i suppose i could just write an init method in the model and then call the init method after.

the reason why i wanted to know was more for simplicity and efficiency.

but another line of code won't hurt i guess. haha.


Problem between using a Model or using a Library - El Forum - 02-01-2008

[eluser]BizComputing[/eluser]
Your other recourse could be to extend the core Model class as my_Model, add the extra argument to my_Model constructor but make sure to call parent::Model with the expected arguments.

Then it's simply a matter of writing your own models to extend my_Model rather than Model.


Problem between using a Model or using a Library - El Forum - 02-01-2008

[eluser]wiredesignz[/eluser]
Code:
$this->load->model('Model_name');

$cat = new Model_name($cat_features);
$dog = new Model_name($dog_features);
$rat = new Model_name($rat_features);

Libraries can be loaded and sub-classed with exactly the same method


Problem between using a Model or using a Library - El Forum - 02-14-2008

[eluser]section31[/eluser]
[quote author="wiredesignz" date="1201921451"]
Code:
$this->load->model('Model_name');

$cat = new Model_name($cat_features);
$dog = new Model_name($dog_features);
$rat = new Model_name($rat_features);

Libraries can be loaded and sub-classed with exactly the same method[/quote]

What exactly is that doing. I was also looking for a method to pass parameters into models. I think it's bizarre how that was left out.


Problem between using a Model or using a Library - El Forum - 02-14-2008

[eluser]wiredesignz[/eluser]
It's creating new model objects from the loaded class, and passing a parameter to the constructor. That is what you were looking for?


Problem between using a Model or using a Library - El Forum - 02-14-2008

[eluser]section31[/eluser]
[quote author="wiredesignz" date="1203048556"]It's creating new model objects from the loaded class, and passing a parameter to the constructor. That is what you were looking for?[/quote]

Might as well just manually include it because it already instantiates it and attaches to the super object using your method.


Problem between using a Model or using a Library - El Forum - 02-14-2008

[eluser]wiredesignz[/eluser]
Ummm... It's not my method, its standard OOP coding practice. It's called instantiation.


Problem between using a Model or using a Library - El Forum - 02-14-2008

[eluser]section31[/eluser]
but aren't you wasting resources by using the $this->load->model('Model_name'); method.

When you won't use the object that's attached to the ci super object.