Reputation: 473
In Codeigniter models is there a way to make this work?
class Mdl_my_model extends CI_Model
{
protected $my_db = NULL;
public function __construct()
{
parent::__construct();
$this->my_db = $this->load->database('my_db', TRUE);
}
public function some_cool_method()
{
$this->load->model('Mdl_other_model');
$this->Mdl_other_model->other_method();
}
}
Can model below make use of the $my_db object or do I have to load db again?
class Mdl_other_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function other_method()
{
//is it possible to use the $my_db object here?
}
}
Upvotes: 0
Views: 150
Reputation: 9265
You don't have to load the database on to a class property. You can simple just load it as you do a model $this->load->database('name')
. It should be accessible to everything beneath when you load it. However I usually just load it in a MY_Controller
on the first line in __construct
to which I extend all my separate controllers off of.
If you are trying to assign different database names to individual properties (which is the only reason why you would assign it as you are doing), you would have to make your other model extend the first and then you can access that property. But this means whenever you load the other model you have to load the first one first. Generally I would also then assign them in to public properties in MY_Controller
controller and the variable would be available throughout.
Upvotes: 2