CodeIgniter Forums

Full Version: Trying to get property of non-object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

My head is spinning and I cannot figure out why I'm getting the Trying to get property of non-object error.

This is the error:
PHP Code:
A PHP Error was encountered

Severity
Notice

Message
Trying to get property of non-object

Filename
models/Crud_model.php

Line Number
924

Backtrace
:

File: /Users/Public/Git/Acme/acme.dev/application/models/Crud_model.php
Line
924
Function: _error_handler

File
: /Users/Public/Git/Acme/acme.dev/application/models/Crud_model.php
Line
968
Function: is_added_by

File
: /Users/Public/Git/Acme/acme.dev/application/views/back/includes_bottom.php
Line
115
Function: is_sale_of_vendor

File
: /Users/Public/Git/Acme/acme.dev/application/views/back/index.php
Line
32
Function: include

File: /Users/Public/Git/Acme/acme.dev/application/controllers/Vendor.php
Line
32
Function: view

File
: /Users/Public/Git/Acme/acme.dev/index.php
Line
292
Function: require_once 

And here comes the method from my Model:


PHP Code:
   function is_added_by($type,$id,$user_id,$user_type 'vendor') {
 
      $added_by json_decode($this->db->get_where($type,array($type.'_id'=>$id))->row()->added_by,true);  
        if
($user_type == 'admin'){
 
           $user_id $added_by['id'];
 
       }
 
       if($added_by['type'] == $user_type && $added_by['id'] == $user_id){
 
           return true;
 
       } else {
 
           return false;
 
       }
 
   
The line 924 is the one with json_decode.

Any help is appreciated!
As far as I can see the only object trying to access a property on that line is "db". Is the database lib loaded?

Or it could also be that 'get_where' failed to find a record in the db in which case row() will return null.
The database is loaded.

Is there any more info/code/log that I can provide here to help debug this issue?
The more I look at it the more certain I am that no matching record is found. I cannot tell you how many times I've been burned by thinking, "Well, I know that table is going to have data that matches the criteria I'm providing." It is wise to not assume anything. Try checking db results before proceeding.
PHP Code:
$query $this->db->get_where($type,array($type.'_id'=>$id))->row();
if(isset(
$query
{
 
   $added_by json_decode($query->added_by,true);  
    
...
}
else
{
 
   //respond to the failure to find data

...and you were right!

Assumption is bad! :-) :-)

Thank you very much!