CodeIgniter Forums
Can't select database using extended controller. regular controller works. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can't select database using extended controller. regular controller works. (/showthread.php?tid=51698)

Pages: 1 2


Can't select database using extended controller. regular controller works. - El Forum - 05-16-2012

[eluser]bill19[/eluser]
Hi everyone,

It appears that in fact it is the ion_auth library which is causing the problem. It looks like there is a bug preventing switching of databases if ion_auth is loaded. I combined the ion_auth tables into my other DB, and now everything is working properly.

Hope this helps someone,

Bill


Can't select database using extended controller. regular controller works. - El Forum - 05-16-2012

[eluser]kptengco[/eluser]
Code:
$DB1 = $this->load->database('default', TRUE);
        $DB2 = $this->load->database('wo', TRUE);

public function listings()
    {
        
        var_dump($this->the_user);
        echo 'in home listing';
        echo "<pre>";
print_r($this->db->list_tables());

echo "<pre>";
print_r($this->DB2);
Change
Code:
print_r($this->db->list_tables());
to
Code:
echo print_r($DB1->list_tables()); or  echo print_r($DB2->list_tables());

You don't have to use
Code:
$this->db->
since you load a database and pass it to a variable by that you'll have to replace
Code:
$this->db->
to your variable
Code:
$DB1->get('table');



Can't select database using extended controller. regular controller works. - El Forum - 05-17-2012

[eluser]Ben Edmunds[/eluser]
So which DB do you want Ion Auth to use?


Can't select database using extended controller. regular controller works. - El Forum - 05-18-2012

[eluser]bill19[/eluser]
Ben,

Thanks for taking a look at this.

I am using the 'default' DB to connect to a separate ion_auth DB, and this works. I want to switch to a second DB entitled 'wo' with config/database file listed above listed above.

Kptengco,

Thank you for the code examples. That helped clarity the syntax for me.

Regards,

Bill


Can't select database using extended controller. regular controller works. - El Forum - 05-18-2012

[eluser]Ben Edmunds[/eluser]
So do you have it working now?

If not, are you saying you want Ion Auth to use "wo" or default"?


Can't select database using extended controller. regular controller works. - El Forum - 05-18-2012

[eluser]bill19[/eluser]
Hi Ben,

My original setup was a database named 'ion_auth' which I created solely for use by your auth system. It contains only the 3 ion_auth tables. my second DB , 'wo' is a wordpress DB , which I wanted to access.

My Plan was to have the user login using the ion_auth DB, then switch to 'wo' ( or at least be able to access it ) I was never able to get this working , so my workaround was to dump the 3 ion_auth tables into 'wo',and set 'wo' as 'default', so I never had to switch DBs.

Using this workaround I was able to get my code working, but I'd still like to know how to switch DBs , if you wouldn't mind.

Bill


Can't select database using extended controller. regular controller works. - El Forum - 05-22-2012

[eluser]Ben Edmunds[/eluser]
You can switch DBs like this:

Code:
$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('wo', TRUE);

and then the key is to overwrite the $this->db object with the db you'd like to use in you're libraries with

Code:
$this->db = $DB2;



Can't select database using extended controller. regular controller works. - El Forum - 05-22-2012

[eluser]bill19[/eluser]
Thanks Ben, it does work providing I set:

Code:
$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('wo', TRUE);

inside a function. I originally had them within the constructor.

Best regards,

Bill


Can't select database using extended controller. regular controller works. - El Forum - 05-22-2012

[eluser]CroNiX[/eluser]
You should be able to define them like that in the constructor, and then use $this->DB1 and $this->DB2 to access them from your methods.

Although, I don't think you need to load this one assuming you are already loading your default database:
Code:
$DB1 = $this->load->database('default', TRUE);
If you are just use $this->db like normal to use your default. You only need to define the 2nd connection.

Code:
class Something extends Something_else {

  public $db_wo;

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

    //Create the 2nd db connection
    $this->db_wo = $this->load->database('wo', TRUE);
  }

  function sample()
  {
    //Access default db
    $data = $this->db->select(...)->get(...)->result();

    //Access wo db
    $wo_data = $this->db_wo->select(...)->get(...)->result();
  }
}
Does that work for you?


Can't select database using extended controller. regular controller works. - El Forum - 05-23-2012

[eluser]bill19[/eluser]
Hi CroNiX,

Yes, that works. Thanks for the explanation.

Bill