CodeIgniter Forums

Full Version: CI_Config::$db Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]pioSko[/eluser]
Hi,
I've have successfully setup a db_sessions+Lang_Selection config and have reached a point where I want the languages array to be filled with those "active" in my mySQL database.

the setup's like this...

autoload.php
Code:
...
$autoload['libraries'] = array('database', 'session', 'lang_detect');
...
$autoload['config'] = array('lang_detect');
...

lang_detect.php
Code:
...
$config['lang_avail'] = array();
$langs_query = $this->db->query('SELECT * FROM `ci_languages` WHERE `active` = 1');

foreach ($langs_query->result_array() as $langs_row) {
    $config['lang_avail'][$langs_row['code']] = $langs_row['code'];
    // print $langs_row['code'];
}
...

$config['lang_default'] = 'en';
$config['lang_selected'] = 'en';
...

now.. what i'm trying to get from the database is something like this:
Code:
$config['lang_avail'] = array(
    'pl' => 'pl',
    'en' => 'en',
    'de' => 'de',
);

BUT, what i get in the browser is this:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Config::$db

Filename: config/lang_detect.php

Line Number: 13

Fatal error: Call to a member function query() on a non-object in E:\Work\Web\WWW\vacaero.com.pl\system\application\config\lang_detect.php on line 13

now, what I'm thinking is that it's trying to access the database ($this->db) before it actually autoloads it (I may be wrong).


Any ideas on how I can go about doing this?

El Forum

[eluser]anouar[/eluser]
Hello ,
I ve got the same problem ,so i have revised the user guide of CI ; it indicate that we should load the necessary librarys.
So i have been obliged to indiacte it in the class autoload as you done,unfortunately the same errors appears.
We have to try the second methode >:-(; load the necessary library in the same class
Code:
$this->load->library('database');
but it does not process.

Maybe :I think that CI does not work with all versions of the data base (Mysql).

Please if you resolve this pb please send us a short message .

Good luck.

El Forum

[eluser]pioSko[/eluser]
*bump*

Anybody? Anything? An idea, perhaps?

El Forum

[eluser]wiredesignz[/eluser]
lang_detect.php being a library has no direct access to the CI super object so `$this` only refers to the library object itself.

Which is why `$this->db` fails.

Do a forum search on accessing the CI super object from a library.

El Forum

[eluser]pioSko[/eluser]
Thank you. That made sense and I have managed to fix it.

lang_detect.php is now:
Code:
...
$config['lang_avail'] = array();

$CI =& get_instance();
$CI->load->database();

$langs_query = $CI->db->query('SELECT * FROM `ci_languages` WHERE `active` = 1');

foreach ($langs_query->result_array() as $langs_row) {
    $config['lang_avail'][$langs_row['code']] = $langs_row['code'];
}
...