CodeIgniter Forums
Trying to get property of non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Trying to get property of non-object (/showthread.php?tid=67730)



Trying to get property of non-object - castle - 04-02-2017

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!


RE: Trying to get property of non-object - dave friend - 04-02-2017

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.


RE: Trying to get property of non-object - castle - 04-02-2017

The database is loaded.

Is there any more info/code/log that I can provide here to help debug this issue?


RE: Trying to get property of non-object - dave friend - 04-02-2017

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




RE: Trying to get property of non-object - castle - 04-02-2017

...and you were right!

Assumption is bad! :-) :-)

Thank you very much!