Page 1 of 1

Need Help....Trying to get property of non-object Error

Posted: Thu Oct 20, 2016 6:53 pm
by joinnavdev
Hi,

I am trying to call getIsAdmin function from another class but getting "Error 500-Trying to get property of non-object"...I am new to PHP so not aware
how to resolve..

I tried to check var_dump also, it's showing "boolean false"

Really looking forward for the help...

Code: Select all

class WebUser
{
    public function getIsAdmin()
    {
        $user = User::model()->findByPk(Yii::app()->user->id);
        return $user->isAdmin == 1;
    }
}

Code: Select all

require_once("WebUser.php");
class ButtonColumn
{
	  function __construct()
     {
		$check= new $WebUser();
		if($check->getIsAdmin()==1)
		{	
			$deleteButtonOptions = array('title' => 'Delete');
			$deleteButtonUrl = 'array("delete", "id" => $data->id)';
		}
	}
}
Thanks,
Nick

Re: Need Help....Trying to get property of non-object Error

Posted: Thu Oct 20, 2016 6:58 pm
by Celauran

Code: Select all

$check= new $WebUser();
Wait, what?

Re: Need Help....Trying to get property of non-object Error

Posted: Fri Oct 21, 2016 1:09 am
by requinix

Code: Select all

$deleteButtonUrl = 'array("delete", "id" => $data->id)';
That also looks suspicious.

Re: Need Help....Trying to get property of non-object Error

Posted: Fri Oct 21, 2016 11:25 am
by joinnavdev
Hi,

First of all thx for your time and help in resolving the issue.

I am just trying to call getIsAdmin function which is another class.....


Below are some images & delete access of records .....are working fine....but I don't want user to have access to delete option...
With admin all is working fine so was trying to call function to check whether the logged one is admin or user....
$deleteButtonUrl = 'array("delete", "id" => $data->id)';..

Also I tried like this
function __construct(WebUser $WebUser) but didn't worked got error..Argument 1 passed to ButtonColumn::__construct() must be an instance of WebUser, instance of GridView given

Hope above information helps..

Re: Need Help....Trying to get property of non-object Error

Posted: Fri Oct 21, 2016 2:51 pm
by joinnavdev
Hi,

By doing below things I could find admin value......

$user = User::model()->findByPk(Yii::app()->user->id);
echo $user['isAdmin'];........o/p:1

if($user['isAdmin'] == '1')

I am getting below error

Error 500

Trying to get property of non-object......

Any help would be appreciated......

Thanks,
Nick

Re: Need Help....Trying to get property of non-object Error

Posted: Fri Oct 21, 2016 3:33 pm
by Christopher
In the code you posted, either User::model() or Yii::app() is not returning an object, or Yii::app()->user is not an object.

Re: Need Help....Trying to get property of non-object Error

Posted: Sat Oct 22, 2016 5:24 am
by joinnavdev
Christopher,

Thx for helping in resolving the issue.

I got your point but what is the way to resolve it as I am able to get the admin value from database but when I am putting in if statement getting error i.e.Trying to get property of non-object.......

I am new to all this so appreciate your help.

Thanks,
Nick

Re: Need Help....Trying to get property of non-object Error

Posted: Sat Oct 22, 2016 3:02 pm
by Christopher
It would be helpful to see the complete error message and to tell us what line the error message is referring to. PHP error messages are very clear. Your error message ("Trying to get property of non-object") means you are doing $foo->bar but $foo is not an object. Usually this is because a function/method is returning null or false instead of an object.