CodeIgniter Forums
why is the $this->db not available in a certain module? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: why is the $this->db not available in a certain module? (/thread-79124.html)



why is the $this->db not available in a certain module? - richb201 - 04-23-2021

In my controller I am using $this->load->database(); which gives me access to $this->db in that controller. I have a php file at /app/assets/MyExport6765.php. But I have no access to $this->db in it. I can see $this in my debugger, but the db is not in there. Is there someway that I can share the same dbase handle from my controller with MyExport6765.php? I need to do some dbase manipulations of a table in that module. 


RE: why is the $this->db not available in a certain module? - wdeda - 04-23-2021

Within Controllers/Models you can use either $db = $this->db; like $db = db_connect(); The ideal is $db = $this->db;
In other types of files $db = $this->db; is not supported, only $db = db_connect(); I'm talking about CI4, obviously.


RE: why is the $this->db not available in a certain module? - richb201 - 04-23-2021

(04-23-2021, 02:23 PM)wdeda Wrote: Within Controllers/Models you can use either $db = $this->db; like $db = db_connect(); The ideal is $db = $this->db;
In other types of files $db = $this->db; is not supported, only $db = db_connect(); I'm talking about CI4, obviously.
Thanks, I am still on CI3


RE: why is the $this->db not available in a certain module? - kenjis - 04-23-2021

I recommend you learn about PHP and OOP.

$this->load->database() creates a database object of CI3 and set it to the property $db ($this->db)
in the controller. Within CodeIgniter's Models, you can also use $this->db.
It is the functionality of CodeIgniter.

If you want to use a database object of CI3 in your MyExport6765.php,
you need to create a database object in it or inject the db object into it.


RE: why is the $this->db not available in a certain module? - InsiteFX - 04-23-2021

PHP Code:
$ci =& get_instance();

$db $ci->db_connect(); 



RE: why is the $this->db not available in a certain module? - richb201 - 04-24-2021

(04-23-2021, 08:43 PM)InsiteFX Wrote:
PHP Code:
$ci =& get_instance();

$db $ci->db_connect(); 
Thanks Insite. I am getting this error:

Type: Error
Message: Call to undefined method Configure::db_connect()
Filename: /app/assets/MyExport6765.php


RE: why is the $this->db not available in a certain module? - wdeda - 04-24-2021

I believe there is a function deviation. The code is self-explanatory: it must be used to connect to a database. Unlike $db = $this->db; which can only be used in Controllers/Models, since "$this->" cannot be used in a non-object context.
Although not recommended if you want to make a query outside the Model/Controller you can use it:
PHP Code:
$db db_connect();
$query $db->table('mytable')
// and so on ... 


https://codeigniter.com/user_guide/database/connecting.html

I insist: I'm talking about CodeIgniter 4!
This option, db_connect();, is exclusive to CI4.

https://codeigniter.com/userguide3/database/connecting.html


RE: why is the $this->db not available in a certain module? - richb201 - 04-24-2021

The problem is that db_connect() is not found. I am on CI3, btw.
I then tried using $db = \Config\Database::connect();
instead but I get Class 'Config\Database' not found


RE: why is the $this->db not available in a certain module? - InsiteFX - 04-24-2021

CodeIgniter 3 uses load database here the code for that one.

PHP Code:
$ci =& get_instance();

$ci->load->database(); 

Not tested but sould be something around that.