![]() |
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-14-2012 [eluser]bill19[/eluser] Hi everyone, I am working on a project where I need to connect to 2 DBs. One is called 'default' , and the second 'wo' . I can't select database using an extended controller, however when I extend a regular CI controller my code works! i.e. Code: class Main extends Common_Auth_Controller Code: class Main extends CI_Controller There are no other changes to the code. In the contructor I am trying to select 'wo' using Code: class Main extends Common_Auth_Controller { I am not sure if this is some kind of bug. Is there anything I can do to more explicitly select the second db? Thank you, Bill Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]weboap[/eluser] extend Common_Auth_Controller to CI_Controller and then try Code: class Common_Auth_Controller extends CI_Controller then Code: class Main extends Common_Auth_Controller Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]bill19[/eluser] Right now, I've got; Code: Main extends Common_Auth_Controller Code: Common_Auth_Controller extends MY_controller Code: MY_controller extends CI_controller Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]CroNiX[/eluser] Are you using an autoloader? CI won't find anything that extends MY_Controller by default. Try putting this at the very bottom of /application/config/constants.php Code: /* MY_Controller.php and Common_Auth_Controller.php would both go in /application/core Any regular controllers that extend those go in the regular /application/controllers dir Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]CroNiX[/eluser] [quote author="bill19" date="1337027097"]Right now, I've got; Code: Main extends Common_Auth_Controller Code: Common_Auth_Controller extends MY_controller Code: MY_controller extends CI_controller [/quote] BTW - it's 'MY_Controller' and 'CI_Controller'. (capital C's for Controller) Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]bill19[/eluser] Hi CroNix, I have all of this setup, as you have suggested. However its still not working. Bill Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]CroNiX[/eluser] Try reading Phils article on this: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]bill19[/eluser] Thanks CroNix, I've actually read that article. ( I got it from you last week ). I have everything setup as in the article. In fact, everything else in the 'Main' controller is working . I was able to check that $this->user which was declared in a parent controller is available in the 'Main' controller, as expected. However I still am not able to change databases. Here are my controllers in more detail: Code: class Main extends Common_Auth_Controller { Code: class Common_Auth_Controller extends MY_Controller { Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); I am stumped , and would appreciate any ideas you may have. Thank you, Bill Can't select database using extended controller. regular controller works. - El Forum - 05-14-2012 [eluser]CroNiX[/eluser] Ah, the problem is in how you are trying to load multiple databases. You can't just switch it like that. See how they do it in the Connecting to Multiple Databases part. $this->db will be your default db as declared in the database config file, and in MY_Controller you can create a $this->db_wo connection or whatever for your 2nd, or if you aren't using it in many places just where you need it. Can't select database using extended controller. regular controller works. - El Forum - 05-15-2012 [eluser]bill19[/eluser] Thanks, however I still seen to be doing something wrong. I have changed my 'Main' controller to add a $DB1 and $DB2 , like so: Code: class Main extends Common_Auth_Controller { my /config/database file looks like: Code: $active_group = 'default'; I am getting the following error: Quote:Message: Undefined variable: DB2 What am I doing wrong? Thanks, Bill |