Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function database() on a non-object
#1

[eluser]yapaarachchi[/eluser]
Hello,

I got following error when I am going to use $this->load->database();. I use Eclipse as my IDE.

Fatal error: Call to a member function database() on a non-object in C:\xampp\htdocs\TestCI\application\models\news_model.php on line 7


And this is my code(../models/news_model.php)

Code:
class News_model extends CI_Model {

public function __construct()
{
  
  $this->load->database();
}

public function get_news($slug = FALSE)
{
  if ($slug === FALSE)
  {
   $query = $this->db->get('news');
   return $query->result_array();
  }
  
  $query = $this->db->get_where('news', array('slug' => $slug));
  return $query->row_array();
}
}



And also I put following code in to autoload.php

Code:
$autoload['libraries'] = array('database');


Can anyone tell how to solve this???
#2

[eluser]InsiteFX[/eluser]
Did you setup database information in ./application/config/database.php ?
#3

[eluser]Aken[/eluser]
You need to add parent::__construct(); as the first thing in your constructor.
#4

[eluser]yapaarachchi[/eluser]
@InsiteFX- Yes, I did that.Thanks buddy Smile

@Aken - I did that as u said but it not works yet. Sad
#5

[eluser]Aken[/eluser]
Then you did it wrong. That should be the first item inside your __construct() method.
#6

[eluser]yapaarachchi[/eluser]
I did as u said friend. But still not working...

Code:
public function __construct()
{
  parent::__construct();
  $this->load->database();
}
#7

[eluser]Aken[/eluser]
Is it the exact same error being displayed? Also, if you're autoloading your database, there's no reason to load it in your model...
#8

[eluser]yapaarachchi[/eluser]
Actually, I am a beginner. This is the code which in the CodeIgniter User Guide. I tried this using Dreamweaver IDE.It works. But when I using Eclipse it is not working. I did all the things which are mentioned by you and by referring the Google.
#9

[eluser]Pedro Luz[/eluser]
if you have this line

Code:
$db['default']['autoinit'] = TRUE;

in your database.php configuration

and in the autoload.php have:

Code:
$autoload['libraries'] = array('database');

you do not need to use

$this->load->database();




Theme © iAndrew 2016 - Forum software by © MyBB