CodeIgniter Forums
Using another database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Using another database (/showthread.php?tid=58721)



Using another database - El Forum - 07-12-2013

[eluser]ninjayan[/eluser]
Hello everyone. This is my code for the ignited datatables..


Code:
$this->load->library('datatables');
  $this->datatables
     ->select("doc_ref, source_doc, subject_doc, enter_doc, date_doc")
     ->from("tbl_doc")
     ->add_column("Action",
        "<form id='disable_form' acti method='post' target='_blank'>
        <input type='hidden' value='$1' id='reference_number' name='reference_number' readonly>
        <button type='submit' class='no_btn' title='View'>
        <img src='$base_dir/assets/img/execute.png' class='left' />
        </button>
        &lt;/form&gt;
        ",
        "doc_ref"
        );
  echo $this->datatables->generate();

I updated CI's db config and added another database connection and I want to use that in the ignited datatables. How can I do that? Thanks in advance!


Using another database - El Forum - 07-12-2013

[eluser]PravinS[/eluser]
check the url

http://ellislab.com/codeigniter/user-guide/database/connecting.html


Using another database - El Forum - 07-12-2013

[eluser]ninjayan[/eluser]
as you can see on my code, there is no
Code:
$this->db->query();

its just $this->datatables->select and its using the default db connection. my question is how can i use the other db connection instead of the default one. i didn't find the solution on the url given. correct me if im wrong


Using another database - El Forum - 07-12-2013

[eluser]PravinS[/eluser]
Search "Connecting to Multiple Databases" in above link page, they have shown how to use two database connections.
But before this you need to add database configuration of your two databases in database configuration file, for that refer following link

http://ellislab.com/codeigniter/user-guide/database/configuration.html

hope this will help you


Using another database - El Forum - 07-12-2013

[eluser]ninjayan[/eluser]
Yes, I understand that. I already configured my database config file to connect to another database. My problem is how to call that db config that will connect to the second database IN IGNITED DATATABLES.

In Ignited Datatables, you query at the controller and not at the model. To fetch some data, we do it like this...

$this->datatables->select(bla bla)->from('table2');

As you can see, it is different from the active record like

$this->db->query(); db1
$this->db_2->query(); db2



Using another database - El Forum - 07-13-2013

[eluser]Unknown[/eluser]
Every time that you wish to use the second db it must be called at the begging of the model or at least in the function you are working on.

First you need to load it like this

Code:
$this->db_2 = $this->load->database('db_2', TRUE);

Then to use it simply

Code:
$this->db_2->query();

Hope this helps. Smile


Using another database - El Forum - 07-13-2013

[eluser]ninjayan[/eluser]
Yes you are right! But I am using Ignited Datatables which the query is done in controller and not in model. That's how it works. As you can see on my code at the top. Smile