CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Third party database config file doesn't load

Open joseadrian opened this issue 13 years ago • 4 comments

I created a package as you mention here with its own config files, including the database config file. There are no folder enviroments.

So, in one controller I did this:

$this->load->model('model_animal');  
$data['species'] = $this->model_animal->species();       

$this->load->add_package_path(APPPATH.'third_party/pokedex/', TRUE);
$this->load->model('model_pokemon'); 
$data['types'] = $this->model_pokemon->types();        
$this->load->view('screen', $data);                  

// third_party/pokedex/models/model_pokemon.php

function __construct()
{
     parent::__construct();
     $this->load->database();
}

function types()
{
    return $this->db->get('type')->result_array(); // doesn't work because it is not using the config file inside the third party package
}

joseadrian avatar Jan 21 '13 21:01 joseadrian

Hey, could you answer the following questions?

  • Which CodeIgniter version are you using? (unless it's 3.0-dev, the functionality that you're looking for simply doesn't exist)
  • Is the database class autoloaded?
  • If not, does model_animal happen to call $this->load->database()?

narfbg avatar Jan 22 '13 00:01 narfbg

Hi!

  • I'm using version 2.1.3
  • The database is not autoloaded. model_pokemon is loading one database.
  • Yes, model_animal is loading the database that way, but it's trying to load another database (since it has its own its own database config file).

Since it is possible to load multiple databases, I assumed that I could load multiple databases even from packages

I was checking the code /system/database/DB.php:

// Is the config file in the environment folder?
if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
{
    if ( ! file_exists($file_path = APPPATH.'config/database.php'))
    {
        show_error('The configuration file database.php does not exist.');
    }
}

So it just checks config files on /APPPATH/config[/enviroment] and not on /APPPATH/third_party/foo/config/[/enviroment].

Maybe is not that common to do what I want to do. Thanks!

joseadrian avatar Jan 23 '13 21:01 joseadrian

The problem is actually unrelated to package paths, and instead is due to you calling $this->load->database() twice. If a proper DB connection has been made (which I'm guessing it has after your first call to that method), the following code will prevent the second from ever processing, regardless of package path:

// Do we even need to load the database class?
if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
{
    return FALSE;
}

Also, as previously stated, 2.1.3 does not have package path support when loading the database (though it could use a little massaging, IMO).

AkenRoberts avatar Jan 24 '13 08:01 AkenRoberts

@joseadrian So upgrade to 3.0-dev then, if you want this functionality.

@cryode Yes, that could be a problem, but if two different databases (as described) are used - I doubt that $return would be FALSE in that expression.

narfbg avatar Jan 24 '13 09:01 narfbg