-
shuimenjian
Newbie
-
Posts: 4
Threads: 1
Joined: Nov 2014
Reputation:
0
11-27-2014, 10:36 PM
I have connected two databases in the config file. I plan to load them in the construct method so that they can be used by other methods. but it's failed and the databases still cannot be used by other methods. it will got the non-object error. I am sure that there is no issue in the databases connection. Because they are normal when load them in each method. Could you please help resolve this issue? Thanks in advance!
The code as below:
PHP Code: class M_db_sync extends CI_Model { function __construct(){ parent::__construct(); $db_local = $this->load->database('default', TRUE); $db_remote = $this->load->database('remote', TRUE); } function get_local_data(){ // $db_local = $this->load->database('default', TRUE); $db_local->order_by("id", "desc"); $db_local->limit(5); $query = $db_local->get('test'); return $query; } function get_remote_data(){ //$db_remote = $this->load->database('remote', TRUE); $db_remote->limit(10); $query = $db_remote->get('Current_Full'); return $query; } }
-
Rufnex
Chuck Norris Mode - Former CI Council
-
Posts: 511
Threads: 7
Joined: Oct 2014
Reputation:
35
I think you have forgott to call db:
PHP Code: class M_db_sync extends CI_Model { function __construct(){ parent::__construct(); $db_local = $this->load->database('default', TRUE); $db_remote = $this->load->database('remote', TRUE); } function get_local_data(){ // $db_local = $this->load->database('default', TRUE); $db_local->db->order_by("id", "desc"); $db_local->db->limit(5); $query = $db_local->db->get('test'); return $query; } function get_remote_data(){ //$db_remote = $this->load->database('remote', TRUE); $db_remote->db->limit(10); $query = $db_remote->db->get('Current_Full'); return $query; } }
-
shuimenjian
Newbie
-
Posts: 4
Threads: 1
Joined: Nov 2014
Reputation:
0
(11-28-2014, 01:04 AM)Rufnex Wrote: I think you have forgott to call db:
PHP Code: class M_db_sync extends CI_Model { function __construct(){ parent::__construct(); $db_local = $this->load->database('default', TRUE); $db_remote = $this->load->database('remote', TRUE); } function get_local_data(){ // $db_local = $this->load->database('default', TRUE); $db_local->db->order_by("id", "desc"); $db_local->db->limit(5); $query = $db_local->db->get('test'); return $query; } function get_remote_data(){ //$db_remote = $this->load->database('remote', TRUE); $db_remote->db->limit(10); $query = $db_remote->db->get('Current_Full'); return $query; } }
no, i have called it in construct method.
$db_local = $this->load->database('default', TRUE);
$db_remote = $this->load->database('remote', TRUE);
-
shuimenjian
Newbie
-
Posts: 4
Threads: 1
Joined: Nov 2014
Reputation:
0
(11-29-2014, 03:23 AM)Rufnex Wrote: I mean the db for the methods like $this->db->limit() ....
It's still failed after db added. Got error with "Undefined variable: db_local", "Undefined variable: db_remote".
-
Rufnex
Chuck Norris Mode - Former CI Council
-
Posts: 511
Threads: 7
Joined: Oct 2014
Reputation:
35
Try this
PHP Code: class M_db_sync extends CI_Model { function __construct(){ parent::__construct(); $this->db_local = $this->load->database('default', TRUE); $this->db_remote = $this->load->database('remote', TRUE); } function get_local_data(){ // $db_local = $this->load->database('default', TRUE); $this->db_local->db->order_by("id", "desc"); $this->db_local->db->limit(5); $query = $this->db_local->db->get('test'); return $query; } function get_remote_data(){ //$db_remote = $this->load->database('remote', TRUE); $this->db_remote->db->limit(10); $query = $this->db_remote->db->get('Current_Full'); return $query; } }
-
shuimenjian
Newbie
-
Posts: 4
Threads: 1
Joined: Nov 2014
Reputation:
0
(11-29-2014, 03:41 AM)Rufnex Wrote: Try this
oh, thank you ! it's passed when $this added and db removed. So the right code as below
PHP Code: class M_db_sync extends CI_Model { function __construct(){ parent::__construct(); $this->db_local = $this->load->database('default', TRUE); $this->db_remote = $this->load->database('remote', TRUE); } function get_local_data(){ // $db_local = $this->load->database('default', TRUE); $this->db_local->order_by("id", "desc"); $this->db_local->limit(5); $query = $this->db_local->get('test'); return $query; } function get_remote_data(){ //$db_remote = $this->load->database('remote', TRUE); $this->db_remote->limit(10); $query = $this->db_remote->get('Current_Full'); return $query; } }
|