Welcome Guest, Not a member yet? Register   Sign In
Problem between using a Model or using a Library
#1

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

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

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

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

[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
#6

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

[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?
#8

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB