Welcome Guest, Not a member yet? Register   Sign In
site_model error
#1

[eluser]davy_yg[/eluser]

Hello,

I have the following codes:

controllers/admin/site.php

Code:
<?php

class Site extends CI_Controller
{
function index()
{
  $data = array();
  
  if($query = $this->site_model->get_records())
  {
   $data['records'] = $query;
  }
  
  $this->load->view('options_view', $data);
}

function create()
{
  $data = array(
   'title' => $this->input->post('title'),
   'content' => $this->input->post('content')
  );
  
  $this->site_model->add_record($data);
  $this->index();
}
}

models/Site_model.php

Code:
<?php

class Site_model extends Model {

function get_records()
{
  $query = $this->db->get('data');
  return $query->result();
}

function add_record($data)
{
  $this->db->insert('data', $data);
  return;
}

function update_record($data)
{
  $this->db->where('id', 14);
  $this->db->update('data', $data);
}

function delete_row()
{
  $this->db->where('id', $this->uri->segment(3));
  $this->db->delete('data');
}

}

I get the following error:


A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$site_model

Filename: admin/site.php

Line Number: 10

Fatal error: Call to a member function get_records() on a non-object in C:\xampp\htdocs\IndonusaCI\application\controllers\admin\site.php on line 10


Line 10: if($query = $this->site_model->get_records())

Why is it?
#2

[eluser]Tpojka[/eluser]
Site model is not loaded.
It has to be loaded before called in code either in autoload.php or in controller.
Also, maybe model should be extending CI_Model not Model if installation is >=1.7.2.
#3

[eluser]davy_yg[/eluser]

I autoload the site_model and now I have a new error message appears:

Fatal error: Call to a member function get_records() on a non-object in C:\xampp\htdocs\IndonusaCI\application\controllers\admin\site.php on line 10

line 10: if($query = $this->site_model->get_records())

how to fix the error ?
#4

[eluser]xtremer360[/eluser]
From what I am understanding of your code as I read it.

This part of code is running a conditional on the variable called query that is what is returned from the function get_records. First off I wouldn't work your code this way. For two reasons. I wouldn't call your variable here as query because when people read these lines of code normally they associate the variable query as the actual query string. I would instead name your variable records.

Code:
if($query = $this->site_model->get_records())
..

First off I would take that whole things out of the conditional as its bad practice to do a function call while assigning the returned value inside of a if statement and then I would do the if statement based on your function I would do something like this instead since you are returning the query result in your model function.

Code:
$records = $this->site_model->get_records();
if (count($records) > 0)
{
...

Reason for this try not to have one line do so much. Its easier to break it down into one thing at a time so that its easier to manage when you have to change your code later.


#5

[eluser]davy_yg[/eluser]

ok, I replace line 10 with:

Code:
$records = $this->site_model->get_records();
  if (count($records) > 0)
  {
   $data['records'] = $query;
  }

and I still have the same error message:

Fatal error: Call to a member function get_records() on a non-object in C:\xampp\htdocs\IndonusaCI\application\controllers\admin\site.php on line 10
#6

[eluser]xtremer360[/eluser]
I just noticed something. This should be the fix. It says that site model isn't an object in which it should so I looked at your model again and spotted something. Notice the CI_ in front of Model.

Code:
class Site_model extends Model {

should be...

Code:
class Site_model extends CI_Model {

#7

[eluser]davy_yg[/eluser]
I did notice that one and already fixed it.
#8

[eluser]xtremer360[/eluser]
So did that correct your error?
#9

[eluser]davy_yg[/eluser]
No, the error still remains.
#10

[eluser]xtremer360[/eluser]
Can you repost all updated relevant code again.




Theme © iAndrew 2016 - Forum software by © MyBB