M
M
Maxim Alekhin2016-07-04 06:08:26
CodeIgniter
Maxim Alekhin, 2016-07-04 06:08:26

Codeigniter: How to get $db array from config/database.php in model?

The config/database.php file contains the $db array with connection options.
How to get this array from the model?
What for? It is necessary to pull out names of databases for simultaneous use in one request.
Example:

select t1.name from db1.table1 t1 left join db2.table1 t2 on (t1.id = t2.t1_id)

You can’t just hardcode it into the code, because database names on different servers are different.
You can use the config, but this info is already in $db, so I would like to pull it up.
$this->db->database
Returns only the current connection.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Alekhin, 2016-07-04
@Settler1

I did not find anything smarter and simpler than this option:

public function __construct() 
        {
            include APPPATH . 'config/' . ENVIRONMENT . '/database.php';
            $this->db_config = $db;
....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question