CodeIgniter Forums
Accessing $this->db from within a model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Accessing $this->db from within a model (/showthread.php?tid=47426)



Accessing $this->db from within a model - El Forum - 12-08-2011

[eluser]Unknown[/eluser]
Hi everybody,

I am using CI 2.1.0 and autoload the database library. Inside a controller, var_dump($this->db) shows the database object. If I load a model inside the controller, var_dump($this->db) inside the model just outputs "bool(false)". However, getting a CI instance (inside the model) and referencing the db object via the CI object:

$this->CI =& get_instance();
var_dump($this->CI->db);

works too. Might I miss something? I expected $this->db to be directly accessible inside a model, like the documentation example shows:
http://ellislab.com/codeigniter/user-guide/general/models.html

I was also trying to load the model this way inside the controller:
$this->load->model('User_model', 'user', true);

But with the same results. The DB connection itself works, I can execute queries, read their results and so on within my model, but by using $this->CI->db, not via $this->db.

Thanks in advance,
Alex.