• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The construct method of Model

#1
Smile 
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! Smile
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;
        }
        
 

Reply

#2
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;
        }
        
 


Reply

#3
(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);        
Reply

#4
I mean the db for the methods like $this->db->limit() ....

Reply

#5
(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".
Reply

#6
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;
        }       
 


Reply

#7
(11-29-2014, 03:41 AM)Rufnex Wrote: Try this

oh, thank you ! it's passed when $this added and db removed. Smile 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
;
 
             
 

Reply

#8
Ah your right .. together we strong Wink

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.