Welcome Guest, Not a member yet? Register   Sign In
Can't select database using extended controller. regular controller works.
#1

[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
-- Does not work

Code:
class Main extends CI_Controller
-- works!

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 {
//class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        
        $this->load->database('wo');
        $this->load->helper('url');

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

#2

[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
#3

[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

#4

[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:
/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
| http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
|
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }

        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}
and
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
#5

[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)
#6

[eluser]bill19[/eluser]
Hi CroNix,

I have all of this setup, as you have suggested. However its still not working.

Bill
#7

[eluser]CroNiX[/eluser]
Try reading Phils article on this: http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY
#8

[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 {
//class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        //do stuff here -
//        remember that $this->the_user is available in this controller
        //as is $the_user available in any view I load

    
        $this->load->database('wo');
//      
        /* ------------------ */

        $this->load->library('grocery_CRUD');

    }

    public function index()
    {
        echo "<h1>Welcome to the world of Codeigniter</h1>";//Just an example to ensure that we get into the function
                die();
    }

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


die();


Code:
class Common_Auth_Controller extends MY_Controller {

    protected $the_user;

     function __construct() {

        parent::__construct();

        if($this->ion_auth->logged_in()) {
            $data->the_user = $this->ion_auth->user()->row();
            $this->the_user = $data->the_user;
            $this->load->vars($data);
        }
        else {
            redirect('/');
        }
    }
}

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class  MY_Controller  extends CI_Controller  {

    function __construct() {
        
            parent::__construct();
//            parent::Controller();
                $this->load->library('ion_auth');
  $this->load->library('session');
  $this->load->library('form_validation');
  $this->load->database();
  $this->load->helper('url');
        }
        
}

I am stumped , and would appreciate any ideas you may have.

Thank you,

Bill
#9

[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.
#10

[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 {
//class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        //do stuff here -
//        remember that $this->the_user is available in this controller
        //as is $the_user available in any view I load

        /* Standard Libraries of codeigniter are required */
//      
//        $this->load->helper('url');
        /* ------------------ */

        $this->load->library('grocery_CRUD');
        
        $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);

my /config/database file looks like:

Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '***';
$db['default']['password'] = '***';
$db['default']['database'] = 'ion_auth';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;

$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['wo']['hostname'] = 'localhost';

$db['wo']['username'] = '***';

$db['wo']['password'] = '*****';
$db['wo']['database'] = '*****92';
$db['wo']['dbdriver'] = 'mysql';
$db['wo']['dbprefix'] = '';
$db['wo']['pconnect'] = TRUE;
$db['wo']['db_debug'] = TRUE;
$db['wo']['cache_on'] = FALSE;
$db['wo']['cachedir'] = '';
$db['wo']['char_set'] = 'utf8';
$db['wo']['dbcollat'] = 'utf8_general_ci';
$db['wo']['swap_pre'] = '';
$db['wo']['autoinit'] = TRUE;
$db['wo']['stricton'] = FALSE;

I am getting the following error:

Quote:Message: Undefined variable: DB2

Filename: controllers/Main.php

Line Number: 43

What am I doing wrong?

Thanks,

Bill





Theme © iAndrew 2016 - Forum software by © MyBB