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();
?>