site_model error |
[eluser]davy_yg[/eluser]
Hello, I have the following codes: controllers/admin/site.php Code: <?php models/Site_model.php Code: <?php 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?
[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.
[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 ?
[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(); 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.
[eluser]davy_yg[/eluser]
ok, I replace line 10 with: Code: $records = $this->site_model->get_records(); 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
[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 {
[eluser]xtremer360[/eluser]
Can you repost all updated relevant code again. |
Welcome Guest, Not a member yet? Register Sign In |