-1

Possible Duplicate:
Call to a member function on a non-object
Call to a member function on a non-object

When i try to access the database using CodeIgniters Active records it always gives me the error

Fatal error: Call to a member function insert() on a non-object

Fatal error: Call to a member function get() on a non-object

below is the settings in my database

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

and i have auto-loaded it

$autoload['libraries'] = array('database','session');

and below is my model

class Leads_model extends CI_Model {
    
function __construct()
  {
    parent::__construct();
  }
    
    function insertq(){
        
        $q=$this->db->insert('test',$data);
        
        if ($q){
            
            return true;
        }       
        else
        {
            return false;
        }
        
        
    }
}

can someone please suggest me what am i doing wrong and why am i getting that error?

Community
  • 1
  • 1
LiveEn
  • 3,193
  • 12
  • 59
  • 104
  • Where are you calling $this->db->get()? Are you sure it refers to this model? – Damien Pirsy Oct 28 '11 at 09:33
  • Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\quest\application\models\leads_model.php on line 14 . I call the $this->db->get() when the form is been submitted. The view loads fine from the controller but when i submit the for i get that error. – LiveEn Oct 28 '11 at 10:03

3 Answers3

2

Are you calling the parent constructor?

class Leads_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
    }
   ...
}
Vikk
  • 3,353
  • 3
  • 22
  • 24
1

It's pretty strange indeed. Make sure: 1) your error refers to that model, and not to another one; 2) you call the __construct() in your Model

If both of this checks are passed, try in-time loading and see if the problem persists:

class Leads_model extends CI_Model {

  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    $this->load->database();
    $data = array('field1' => 'value1','field2' => 'value2');
    $this->db->insert('test',$data);
  }

  function run_test()
  {
     $dbo = $this->load->database('default',TRUE);
     $query = $this->$dbo->get('tablename');
     echo $query->num_rows();
  }

}

You can also see if the functions returns the connection id, as a test:

  $dbobject = $this->load->database('default',TRUE);
  var_dump($dbobject->conn_id);
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • when i dump i get "resource(33) of type (mysql link persistent)" – LiveEn Oct 28 '11 at 10:11
  • Ok, so the class works perfectly, and the connection is established. Could you try running the `run_test()` function I suggested above AS IS (just changing to your tablename, obv). See if it still gives errors – Damien Pirsy Oct 28 '11 at 10:27
  • i added some variables to the model core to get the auto complete work. after i remove that everything works fine. Thanks a lot for the help. but the auto complete in eclipse doesn't work. – LiveEn Oct 28 '11 at 10:32
  • Change IDE or start typing out of habit then :D And beware when you change cores! – Damien Pirsy Oct 28 '11 at 10:37
0

The problem was i had added some variables to get the autocomplete work in to the model core. That was causing the problem and its working after removing those.

but the auto complete doesn't seem to work now.

LiveEn
  • 3,193
  • 12
  • 59
  • 104