Welcome Guest, Not a member yet? Register   Sign In
db object is gone
#1

[eluser]Daniel_C[/eluser]
Hello everyone, thanks for the answers.

Well, I'm trying to build my own extended library to auto detect language used by the user.
This is my code:
Code:
<?php
class MY_Language extends CI_Language {
    function MY_Language() {
        parent::CI_Language();
        include_once(APPPATH . 'config/autoload' . EXT);
        
        //detect language using the headers
        //...
        //
        $lang = 'spanish';
        
        $file = $autoload['language'];
        
        if (!is_array($file)) {
            $file = array($file);
        }
        
        foreach ($file as $langfile) {    
            $this->load($langfile, $lang);
        }
    }
}

I did not find the way to read autoload values, so the code include the script, but when I do that and use the db library, it throws me an error.
Controller has this code
Code:
$this->db->select('rg.nombre_corto, rr.nombre');

And when I look at that controller, PHP shows me this error:
Fatal error: Call to a member function select() on a non-object

It's weird because I have database for autoloading in the autoload configuration. When I put a comment in the line of inclusion the database is correctly loading.

Thanks.
#2

[eluser]WanWizard[/eluser]
$this->db is not available in libraries. You need to use get_instance() to access the CI superobject.
#3

[eluser]Daniel_C[/eluser]
I am using the code with the $this->db in a Controller:
system/application/controller/main.php

The problem is that when I include the autoload.php in the library, PHP shows the fatal error when accessing the controller at localhost/main. It's like a kind of bug.
#4

[eluser]WanWizard[/eluser]
Probably. But as I don't have a clue to what your exactly doing, I can't comment on that.

A config file only contains some variable definitions, and no code. So it can't influence CI in any way, you include it in the local scope of a class method.
#5

[eluser]Daniel_C[/eluser]
I checked that the problem is behind the order of loading the libraries, the thing that I was thinking is that my extended library (MY_Language) is first loaded, the library do the inclusion of the autoload.php but then when the framework charges the Loader library we can saw this code:
Code:
function _ci_autoloader()
{    
    include_once(APPPATH.'config/autoload'.EXT);
    
    if ( ! isset($autoload))
    {
        return FALSE;
    }

Because the autoload.php was loaded in the MY_Language.php file, the _ci_autoloader method is unable to load it again and return FALSE because the file was include in other scope. If we change the include_once for an include the issue disappear.

The summary is that we can not include autoload.php file or I thought that other files configuration within extended libraries that is first loaded because it could influence the CI Wink

(The clean solution for my case was to made an extra configuration file with the autoload attribute that I needed) I forget that get_instance function doesn't work on the extended language library so I can't use $this->config->item() to get values from configuration file, and the dirty one was modifying the core (include_once for include)




Theme © iAndrew 2016 - Forum software by © MyBB