Codeigniter database connect to multiple databases in model -
to bridge gap between older system , codeigniter, have extended ci_model in ci 2. here's constructor:
public function __construct ($pkname='', $tablename='') { parent::__construct (); $this->conf['pkname'] = $pkname; //name of auto-incremented primary key $this->conf['tablename']= $tablename; //corresponding table in database $this->dbr = $this->load->database('read', true); $this->dbw = $this->load->database('write', true); // echo $this->dbr->conn_id // echo $this->dbw->conn_id }
if uncomment 2 lines echoing conn_id, different each time. shouldn't re-using connection information? i'd think means hitting same resource #id each time. have pconnect=true both.
resource id #24 resource id #25 ... snip ... resource id #127 resource id #128
i'm in dev it's not big issue don't want go production connecting separately each model. thanks.
the db_manager class in forum post solved problem:
http://codeigniter.com/forums/viewthread/157298/
i autoload db_manager library use in my_model class extends ci_model
if (!$dbh = $this->db_manager->get_connection($type)) { // throw exceptions etc } $r = mysql_query ($query, $dbh->conn_id);
i'm using mysql_query right because i'm converting different system , don't want rewrite models right now. otherwise use ci active class stuff.
works well.
Comments
Post a Comment