Error - Trying to get property of non-object

Hi, i have an error: "Trying to get property of non-object"

My question is: Why does Frontend work and not for Backend?

This is the function to get the value of a role:




public static function getUserRolValor($userId=null)

{

		if($userId==null){

			$usersRolValor = Yii::$app->user->identity->rol->rolValor;

			return isset($usersRolValor) ? $usersRolValor : false;

		} else {


			$user = User::findOne($userId); //The Error appears in this line


			$usersRolValor = $user->rol->rolValor;

			return isset($usersRolValor) ? $usersRolValor : false;

			//return $userRolValor->rol ? $userRolValor->rol->rolValor : '- no role -';

		}

}



This is the function to obtain a role:




public static function requerirMinimoRol($rolNombre, $userId=null)

{

		if(ValorHelpers::esRolNombreValido($rolNombre)){

			if($userId == null){

				$userRolValor = ValorHelpers::getUserRolValor();

			}else{

				$userRolValor = ValorHelpers::getUserRolValor($userId);

			}

			

			return $userRolValor >= ValorHelpers::getRolValor($rolNombre) ? true : false;

		}else{

			return false;

		}

}



And This is my layout/main (just a part)




<?php

	$esAdmin = PermisosHelpers::requerirMinimoRol('Admin');

	

  	if (!Yii::$app->user->isGuest && $esAdmin){	

 

   		NavBar::begin([

   				'brandLabel' => 'NanoSoft <i class="fa fa-plug"></i> Admin',

    			'brandUrl' => Yii::$app->homeUrl,

    			'options' => [

           			'class' => 'navbar-inverse navbar-fixed-top',

      			],

  		]);

 

  	} else {

  		NavBar::begin([

  				'brandLabel' => 'NanoSoft <i class="fa fa-plug"></i>',

      			'brandUrl' => Yii::$app->homeUrl,

      			'options' => [

           			'class' => 'navbar-inverse navbar-fixed-top',

      			],

  		]);    

  		$menuItems = [

  				['label' => 'Home', 'url' => ['site/index']],

  		];

  	}

	if (!Yii::$app->user->isGuest && $esAdmin) {

		$menuItems[] = ['label' => 'Usuarios', 'url' => ['user/index']];

            

      	$menuItems[] = ['label' => 'Perfiles', 'url' => ['frontend/views/perfil/index']];

            

      	$menuItems[] = ['label' => 'Roles', 'url' => ['rol/index']];

                

      	$menuItems[] = ['label' => 'Tipos de Usuario', 'url' => ['tipo-usuario/index']];

           

      	$menuItems[] = ['label' => 'Estados', 'url' => ['estado/index']];

 

   	}

  	if (Yii::$app->user->isGuest) {

  		$menuItems[] = ['label' => 'Login', 'url' => ['site/login']];

 	} else {

 		$menuItems[] = [

 				'label' => 'Logout (' . Yii::$app->user->identity->username . ')',

 				'url' => ['/site/logout'],

                'linkOptions' => ['data-method' => 'post']

		];

 	}                                                                                                             

  	echo Nav::widget([

  			'options' => ['class' => 'navbar-nav navbar-right'],

      		'items' => $menuItems,

  	]);

 

  	NavBar::end();

 

?>



Post error along with stacktrace. Stacktrace show where exactly error happened and why.

7231

Error Yii2.png

There are several question that you could ask…

Did you find a valid user?

Is it null? Or valid?

What does usersrolvalor print?

You need to debug :)

Is a Valid User, because in frontend I can login.

Is not empty, I did the tests in MySQL

usersrolvalor represent a valor of an user, for example:

rolNombre ‘User’, rolValor ‘10’;

rolNombre ‘Admin’, rolValor ‘20’;

On line 43, one of those are non objects.

In programming, do not assume anything :)

Okay, I remove:


$usersRolValor = $user->rol->rolValor;

to


$usersRolValor = $user->rol;

but the idea is control the acces to backend whit any value of user…

I test and work. But in the layout how do I control it?