Welcome Guest, Not a member yet? Register   Sign In
Database instance related to model
#1

[eluser]veledrom[/eluser]
Hi,

When a unique user call model below, does it always create a new instance of database or every call to it uses one (first time created) instance?

Thanks


Code:
class Test_model extends CI_Model {

public function __construct()
{
  parent::__construct();
  
  $this->load->database();
}

public function get_year($id)
{
  $this->db->protect_identifiers('year');
  
  $sql = "SELECT FROM year WHERE id = ?";
  
  $data['dbquery'] = $this->db->query($sql, array($id));
  
  return $data['dbquery'];
}
}
#2

[eluser]DarkManX[/eluser]
A htttp-request make your server create instances of the classes and connect to the db - every single time.
#3

[eluser]veledrom[/eluser]
[quote author="DarkManX" date="1343474565"]A htttp-request make your server create instances of the classes and connect to the db - every single time.[/quote]

Is CI not using singleton-connection per user? I mean if user X calls 50 different models (with db connection in them), does it create 50 instances and connections to db? Does it not use already open connection?
#4

[eluser]DarkManX[/eluser]
I just looked up in the db-drivers, there is no sign of using singleton patter.
#5

[eluser]InsiteFX[/eluser]
Autoload the database.
#6

[eluser]veledrom[/eluser]
[quote author="InsiteFX" date="1343481543"]Autoload the database.
[/quote]

Does database get autoloaded if I set to autoload database, when I call any model, controller (those ones never need database processing)?

"The "auto connect" feature will load and instantiate the database class with every page load" - This will slow my application down though because I don't need database processing in every single page so it is not good practice.
#7

[eluser]PhilTem[/eluser]
As you probably got confused with the terms singleton, auto-load and others, let me try to shed some light (hope I do it all correctly):

When a person browses your domain, one specific controller is invoked and everything that it needs to load is loaded. If you load the database via
Code:
$this->load->database();

there will be one instance under $this->db of your database connection. You can load as many models afterwards, they will all use that one $this->db which we created previously by loading the database.

However, if the person browsing your page opens two tabs and loads two of your pages at the same time, you basically get two database objects, but they are completely isolated from each other. Furthermore that means, there is no persistence between two page requests (neither at the same time nor one chronologically executed after another).
Though you can set the database connection to persistent it does not mean that you will always have one and the same database object. It's always a similar object but not the very same. The process of using persistent connections has nothing to do with persistent objects (which are by nature impossible to have in PHP)

To sum it up:
Once you load the database somehow ($this->load->database() or by autoload.php) you have on database object that serves all models, libraries, controllers, helpers, ... but only until the page is created and sent to the user's browser. At that time the PHP "garbage collection" runs and everything that remains from your prior script execution is deleted. It's actually that simple Wink
#8

[eluser]veledrom[/eluser]
[quote author="PhilTem" date="1343516491"].......To sum it up:
Once you load the database somehow ($this->load->database() or by autoload.php) you have on database object that serves all models, libraries, controllers, helpers, ... but only until the page is created and sent to the user's browser. At that time the PHP "garbage collection" runs and everything that remains from your prior script execution is deleted. It's actually that simple Wink[/quote]

So there is no real difference between auto-load or manual load in terms of performance. However, it would be ideal to use manual connections if most of my pages do not need database processing.

Do I understand right?

Thanks
#9

[eluser]PhilTem[/eluser]
I don't think there is any performance issue especially if you need the DB only for one model since it will the db-class will be loaded anyway. That means, if you have 1000000 models that use the database or just one, regarding the performance of the database class (loading and instantiation) there is no difference - it will only be done once. However, the number of 1000000 models will slow down your system since every single on of it will be loaded and assigned everything that is already loaded (like all previously loaded models, the input, benchmark, output, config class and all that stuff).

If most of your pages don't need a database connection I personally would load the database driver only on the pages that need it. However, if you use the session class with database-storage it's quite obvious that the database will be loaded anyway on every page request. Keep that in mind Wink
#10

[eluser]veledrom[/eluser]
[quote author="PhilTem" date="1343593613"].........However, if you use the session class with database-storage it's quite obvious that the database will be loaded anyway on every page request. Keep that in mind Wink [/quote]

So if I'm using database session, database loads automatically, yes sounds obvious but I never though of this one!!!

Thanks for all these info. Much appreciated




Theme © iAndrew 2016 - Forum software by © MyBB