简体   繁体   中英

Error when attempting to connect to a non-default database in Codeigniter

Here is my database configuration for the database I'm attempting to connect to. It is different for this particular model.

$db['campaignsDB']['hostname'] = 'localhost';
$db['campaignsDB']['username'] = 'root';
$db['campaignsDB']['password'] = '';
$db['campaignsDB']['database'] = 'sf_campaigns';
$db['campaignsDB']['dbdriver'] = 'mysql';
$db['campaignsDB']['dbprefix'] = '';
$db['campaignsDB']['pconnect'] = TRUE;
$db['campaignsDB']['db_debug'] = TRUE;
$db['campaignsDB']['cache_on'] = FALSE;
$db['campaignsDB']['cachedir'] = '';
$db['campaignsDB']['char_set'] = 'utf8';
$db['campaignsDB']['dbcollat'] = 'utf8_general_ci';
$db['campaignsDB']['swap_pre'] = '';
$db['campaignsDB']['autoinit'] = TRUE;
$db['campaignsDB']['stricton'] = FALSE;

I have created a crud_model and I'm sure the functions are written right, but I'm going to include the relevant part...

class CRUD_model extends CI_Model
{
    protected $database = null;
    protected $_table = null;
    protected $_primary_key = null;

    public function __construct()
    {
        parent::__construct();
        $this->load->database($this->database, TRUE);
    }

What I'm having a problem with is as follows:

I create a campaigns_model...

class Campaigns_model extends CRUD_model
{
    protected $database = "campaignsDB"; //DEFINE database
    protected $_table = "camp_forminfo";
    protected $_primary_key = "form_id";

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

Now I'm returning to object oriented programming after a long hiatus so it could be that I'm missing some important part here, but I could have sworn that by defining a variable of a base class in a derived class it would thus be defined when I try to use the base class' method. I attempt to use the CRUD_model's insert function in my controller...

First I load the campaigns_model...

class Campaigns extends MY_Controller
{
    private $layout = "";
    private $content = "";

    public function __construct()
    {
        parent::__construct($this->layout);
        $this->load->model('campaigns_model');  
    }

Then I use it in my controller function...

public function saveForm()
{
    $this->campaigns_model->insert([
        'form_name' => $this->input->post('form_name'),
        'form_leads' => $this->input->post('form_leads'),
        'form_content' => $this->input->post('form_content'),
        'form_template' => $this->input->post('form_template')  
    ]);
}

I get the following error:

An Error Was Encountered

You have not selected a database type to connect to.

I might be wrong, but this is how I do it within my double database apps:

class CRUD_model extends CI_Model{
    protected $database = null;
    protected $_table = null;
    protected $_primary_key = null;

    public function __construct() {
        parent::__construct();
        $this->db = $this->load->database($this->database, true);
    }

Strangely enough I was able to make this all work with a slight modification to my CRUD_model code.

By changing this...

class CRUD_model extends CI_Model
{
    protected $database = null;
    protected $_table = null;
    protected $_primary_key = null;

public function __construct()
{
    parent::__construct();
    $this->load->database($this->database, TRUE);
}

To this...

class CRUD_model extends CI_Model
{
    protected $database = null;
    protected $_table = null;
    protected $_primary_key = null;

public function __construct()
{
    parent::__construct();
    $this->load->database("".$this->database."", TRUE);
}

It worked... by forcing that variable to be interpreted as a string it worked. Why that is, I've yet to learn, perhaps someone can share?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM