Welcome Guest, Not a member yet? Register   Sign In
New User - question on $this->load->database()
#1

[eluser]apodner[/eluser]

Hello all,

I am just trying out CI for the first time with any seriousness, and trying to move an existing app into the CI framework. I have shut off persistent connections, as I am not a fan of those.

I see where the manual suggests that the models you create extend the CI_Model class. In my app, multiple models could be instantiated in a single view, and I assume that if each model calls $this->load->database(), then I am creating multiple connections to the database in a single script execution, which I would prefer to not do.

It seems like less overhead to call the database connection in the controller and inject it into each model as a constructor parameter when the model(s) is instantiated.

My question is, am I losing anything by not extending the CI_Model class? I suppose it really doesn't matter since CI_Model does not appear to automatically create a connection to the database anyway, so the dependency injection would work either way.

Just wanted to see what veteran users are doing and if you all have any tips or suggestions regarding this.

thanks in advance
#2

[eluser]satej[/eluser]
You can use autoload config file to autoload the database library instead of calling it each time

http://ellislab.com/codeigniter/user-gui...oader.html
Hope it helpsSmile
#3

[eluser]apodner[/eluser]
Appreciate the reply, but it seems as though autoload will make a DB connection, even on a page that doesn't require one. I guess I am just a bit of a control freak, I only want the db connection when I need it, and I only want it opened one time per page.

However, this may be moot, as I am not seeing a way to pass the db object into the model constructor via a parameter using $this->load->model('Mymodel'). Seems like it would work if it was a library or if I establish a separate connect method in the model, which seems like overkill as well.

Thanks again!



#4

[eluser]PhilTem[/eluser]
If you load a model requiring a database connection (i.e. loading the DB AR class and connecting) and then load a second model requiring a database connection it will not create neither a second database AR object nor a second connection. Here the singleton approach comes into play which means there is just one database object connecting and selecting one database. It doesn't even matter if you load the database driver with
Code:
$this->load->database();
or in autoload. There is just one connection.

PS: It's the same with loading a model twice. You don't get two objects instantiated, there will just be one instantiation on the first call afterwards the very same object will be returned Wink
#5

[eluser]apodner[/eluser]
Awesome, that is exactly the answer I was looking for! did not realize the database driver class was set up as a singleton.

Perfect, and thanks for taking the time to respond!

AP
#6

[eluser]ahmed.alsiddig[/eluser]
thanks




Theme © iAndrew 2016 - Forum software by © MyBB