DB obj passed from controller to model |
[eluser]Rahi[/eluser]
Hi, I am encountering a situation where I have two database objs created in controller constructor. Now when I load model I have to pass database objs to model so that I don't have to create db instance again in the model. My code will surely clear the situation what I am talking about. Controller Code: class Team_Users extends Controller { Model Code: class Team_Users_Model extends MY_Model { And of course the extended model which is throwing error MY_Model Code: abstract class MY_Model extends Model { Now error comes at $this->db->table_exists($table) I know I can create db instance in this extended model. But I don't want to create that. I want to use existing one which is created in Controller. Any help will be appreciated.
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...cting.html Quote:Connecting to Multiple Databases So... Code: if ($this->ci->sourceDB->table_exists($table)) {
[eluser]summery[/eluser]
If you're connecting to multiple databases, the syntax is a little different. From CI's amazing documentation, you should be doing something like the following: Code: $DB1 = $this->load->database('group_one', TRUE); Then when you call a query, Code: $DB1->query(); In other words, $this->db won't work - you need to refer to the database connection's name in order to tell CI which database you want. Good luck!!
[eluser]Rahi[/eluser]
@summery and @nortrum Thanks for your reply. I have gone through this before I created Model and Controller. I have just found what was wrong. If you see my controller I had implemented multiple databases(same what you suggested from documentation). Now I just moved those object to model and now everything is fine. Controller Code: class Team_Users extends Controller { Model Code: abstract class MY_Model extends Model { |
Welcome Guest, Not a member yet? Register Sign In |