DB obj passed from controller to model - El Forum - 02-09-2010
[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 {
public $sourceDB = '';
public $destinationDB='';
function __construct(){
parent:: __construct();
$this->sourceDB = $this->load->database("db1", TRUE);
$this->destinationDB = $this->load->database("db2", TRUE);
$this->load->model("team_users_model", "", $this->destinationDB);
}
}
Model
Code: class Team_Users_Model extends MY_Model {
function __construct() {
parent::__construct();
// Sets data of table associated to the model
$this->setTableData('team_users');
// Sets validation rules for the model
$this->setValidation(
array(
'first_name' => 'required',
'last_name' => 'required',
'user_name' => 'required',
'password' => 'required',
'email' => 'required|valid_email'
)
);
}
}
And of course the extended model which is throwing error
MY_Model
Code: abstract class MY_Model extends Model {
protected $table = ''; // table associated to the model
protected $fields = array(); // fields of table associated to the model
protected $errors = array(); // model errors
protected $tablePrefix = "ih_";
/**
* Constructor
* @access protected
*/
protected function __construct() {
parent::Model();
// get CI superobject as a model property
$this->ci =& get_instance();
}
public function setTableData($table = 'default') {
if ($this->db->table_exists($table)) {
$this->table = $this->tablePrefix.$table;
$this->fields = $this->db->field_names($this->table);
}
}
}
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.
DB obj passed from controller to model - El Forum - 02-09-2010
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-guide/database/connecting.html
Quote:Connecting to Multiple Databases
If you need to connect to more than one database simultaneously you can do so as follows:
Code: $DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Note: Change the words "group_one" and "group_two" to the specific group names you are connecting to (or you can pass the connection values as indicated above).
By setting the second parameter to TRUE (boolean) the function will return the database object.
When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:
Code: $this->db->query();
$this->db->result();
etc...
You will instead use:
Code: $DB1->query();
$DB1->result();
etc...
So...
Code: if ($this->ci->sourceDB->table_exists($table)) {
...
}
DB obj passed from controller to model - El Forum - 02-09-2010
[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);
$DB2 = $this->load->database('group_two', TRUE);
Then when you call a query,
Code: $DB1->query();
$DB1->result();
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!!
DB obj passed from controller to model - El Forum - 02-09-2010
[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 {
public function __construct(){
parent:: __construct();
$this->load->model("team_users_model", "team");
}
public function get_team_users(){
$where =array("active"=>1);
$this->team->select_team_users($where);
// print("YESY");
var_dump(MY_Model::$destinationDBObj);
}
}
Model
Code: abstract class MY_Model extends Model {
protected $table = ''; // table associated to the model
protected $fields = array(); // fields of table associated to the model
protected $tablePrefix = "ih_";
protected static $sourceDBObj=null;
protected static $destinationDBObj=null;
/**
* Constructor
* @access protected
*/
protected function __construct() {
parent::Model();
self::$sourceDBObj = $this->load->database("db1", TRUE);
self::$destinationDBObj = $this->load->database("db2", TRUE);
// get CI superobject as a model property
$this->ci =& get_instance();
}
public function setTableData($table = 'default') {
if (self::$destinationDBObj->table_exists($table)) {
$this->table = $this->tablePrefix.$table;
$this->fields = self::$destinationDBObj->list_fields($this->table);
}
}
}
|