[eluser]Denzel[/eluser]
Maybe I'm doing something the wrong way, I've only been using this framework for a month.
The Problem
Code:
class news_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database('news');
}
public function get_news() {
// blah
}
}
class comments_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database('comments');
}
public function get_comments() {
// blah
}
}
class ultra_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->model('comments_model');
// COLLISION
$this->news_model->get_news();
}
}
As you can see, due to the use of active records,
get_news will attempt to execute upon the comments database.
My Solution
Code:
class news_model extends CI_Model {
protected $db = NULL;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->news_db = $this->load->database('news');
$this->db =& $CI->news_db;
}
public function get_news() {
// blah
}
}
class comments_model extends CI_Model {
protected $db = NULL;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->comments_db = $this->load->database('comments');
$this->db =& $CI->comments_db;
}
public function get_comments() {
// blah
}
}
class ultra_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->model('comments_model');
// PEACHY
$this->news_model->get_news();
}
}
This is how I alleviate the problem. Note, I add a member variable to CodeIgniter to retain profiling data. (CodeIgniter finds open database connections through reflection.) Am I going about this the wrong way, or is this simply a valid workaround for an active record limitation? Thanks.