Welcome Guest, Not a member yet? Register   Sign In
dbforge multiple databaes problem
#1

[eluser]ghst[/eluser]
Hi! i have problem with DBforge class.
Error is: No database selected

I'm using HMVC, and each of my modules can have it own database config and i dont have default database settings in application/config/database config file, instead of it i do like this:

Code:
$this->DDB = $this->load->database($this->config->item('donate'), TRUE);

where config item has all database params, but when i try to create a table it gives me error, but i still can use $this->DDB to query database.

My code:

Code:
function check_table()
{
    if ( ! $this->DDB->table_exists($this->config->item('active_table', 'donate')))

        $this->load->dbforge();

        $structure = array(
            'id' => array(...)
        );

        $this->dbforge->add_field($structure);
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table($this->config->item('active_table', 'donate'));
I searched in forums and tried to do this:

Code:
$this->DDB->query('use ' . 'donate' );

before dbforge loading, but it doesent helps, also tried to load database via DSN string, but same error. i think it is because of dbforge tries to search in default database.
#2

[eluser]ghst[/eluser]
bump
#3

[eluser]Piter[/eluser]
Hi,

Try this,

This is not consistent with the operation of the framework, but always a workaround.

Create new library: dbforge.php

Code:
<?php
include ('system/database/DB_forge.php');


class Dbforge extends CI_DB_forge{

public function __construct()
{
  parent::__construct();
}

public function set_database($db)
{
  $this->db =& $db;
}
}

?>

$db is database object.

[code]
$this->load->library('dbforge');
$this->dbforge->set_database($this->db);
$this->dbforge->some_function();
[/code]
#4

[eluser]ghst[/eluser]
[quote author="Piter" date="1330426268"]Hi,

Try this,

...

[/quote]

Hi.
Tried but got error.

Fatal error: Call to undefined method CI_DB_mysqli_forge:Confusedet_database() in /var/www/.../application/modules/donate/models/donate_model.php on line 30

Where line 30 is
Code:
$this->dbforge->set_database($this->DDB);


Full code of what i did:


In model constructor i created database object like this:
Code:
$this->DDB = $this->load->database($this->config->item('donate'), TRUE);


Then in function i do this:

// Loading workaround for dbforge
// Library is located in /application/libraries/dbforge.php
Code:
$this->load->library('dbforge');

$this->load->dbforge();
$this->dbforge->set_database($this->DDB);
...

Model is located in "module" directory


#5

[eluser]Piter[/eluser]
You is not loading: $this->load->dbforge();

Load double, his first version then the original,
charge without the original:

Remove this:

Code:
$this->load->dbforge();
The controller has to be just that:
Code:
$this->load->library('dbforge');
$this->dbforge->set_database($this->DDB);
#6

[eluser]ghst[/eluser]
Ok, now i got
Fatal error: Call to undefined method Dbforge::_create_table() in /var/www/.../shop/system/database/DB_forge.php on line 193
#7

[eluser]toopay[/eluser]
If you use MySQL, you could update your code based by latest branch, if i remember correctly, someone manage to adding select_db method within native MySQL driver, but if you use PDO or other driver(s), it looks like, you need to re-initialize the forge, and the driver (by assign DB instance into CI singleton), each time you switch the database.
Code:
// From controller scope
$DB1 = $this->load->database('one');
$this->db = $DB1;
$this->load->db_forge();
//....
//....
$DB2 = $this->load->database('two');
$this->db = $DB2;
$this->load->db_forge();

There are some exception, i notice, regarding CI as a framework. It works as a single, cohesive component(s) rather than provide lousely coupled library/class(es). In the past, i have this pull, which exactly perfect at this scenario.

But apparently, no one have interest to change this behaviour. Off course, one could easily extending the whole functionality (or most of it) by extending the Core class, which is mostly will be the only choice, if we are working in large codebase that sue flexibility and "modular" logic.
#8

[eluser]Piter[/eluser]
For MYSQL database
Code:
<?php
require_once(BASEPATH.'database/DB_forge.php');
require_once(BASEPATH.'database/drivers/mysql/mysql_forge.php');

class Dbforge extends CI_DB_mysql_forge{

public function __construct()
{
  parent::__construct();
}

public function set_database($db)
{
  $this->db =& $db;
}
}

?>
You should be OK
#9

[eluser]ghst[/eluser]
I restarted apache and now all works with Piter workaround. Thanks to Piter.


No looks like it doesent work. I deleted table and now got

Fatal error: Call to undefined method Dbforge::_create_table() in /var/www/.../shop/system/database/DB_forge.php on line 193

Will try latest piter code and will try to update branch.

EDIT.

Very weird, but with require once now looks like it works. Tried to delete table and it creates it again. Thank you all.




Theme © iAndrew 2016 - Forum software by © MyBB