Thanks I appreciate your help:
in C:\var\www\yiitry\backend\views\user\index.php at line 36
refers to:
['label'=>'Role', 'value'=>function ($model, $index, $widget) { return $model->role->role_name; }],
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
I set up a relationship between User and Role as I outlined in my post. I did something similar with Profile and Gender and it worked perfectly. The only differences that I can see are that User resides in Common/models/User and Role is in backend/models/Role. On the Profile/Gender relationship, they are in the same frontend/models folder. The other thing that is different is obviously the Yii template User model has code specific to it and does not include the attribute labels. I tried putting them in to see if it made a difference, but it didn’t.
There was a constant for role in the original template, but I removed it so I could use the table instead and it works perfectly until I try to use the relationship. The dropdowns for update user work perfectly, so it is referencing the relationship correctly from backend/views/user/_form. Here is the common/models/User user model:
<?php
namespace common\models;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
use yii\db\Expression;
use backend\models\Role;
use backend\models\Status;
use yii\helpers\ArrayHelper;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property string $auth_key
* @property integer $role
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
* @property string $password write-only password
*/
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_ACTIVE = 10;
public static function tableName()
{
return 'user';
}
/**
* Creates a new user
*
* @param array $attributes the attributes given by field => value
* @return static|null the newly created model, or null on failure
*/
public static function create($attributes)
{
/** @var User $user */
$user = new static();
$user->setAttributes($attributes);
$user->setPassword($attributes['password']);
$user->generateAuthKey();
if ($user->save()) {
return $user;
} else {
return null;
}
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => new Expression('NOW()'),
],
];
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::find($id);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::find(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
$expire = \Yii::$app->params['user.passwordResetTokenExpire'];
$parts = explode('_', $token);
$timestamp = (int)end($parts);
if ($timestamp + $expire < time()) {
// token expired
return null;
}
return static::find([
'password_reset_token' => $token,
'status' => self::STATUS_ACTIVE,
]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return Security::validatePassword($password, $this->password_hash);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Security::generatePasswordHash($password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Security::generateRandomKey();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Security::generateRandomKey() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
[['status'],'in', 'range'=>array_keys($this->getStatusList())],
['role', 'default', 'value' => 10],
[['role'],'in', 'range'=>array_keys($this->getRoleList())],
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique'],
];
}
/**
* get role relation
*
*/
public function getRole()
{
return $this->hasOne(Role::className(), ['id' => 'role']);
}
/**
* get role name
*
*/
public function getRoleName()
{
return $this->role->role_name;
}
/**
* get list of roles for dropdown
*/
public static function getRoleList()
{
$droptions = Role::find()->asArray()->all();
return Arrayhelper::map($droptions, 'id', 'role_name');
}
/**
* get status relation
*
*/
public function getStatus()
{
return $this->hasMany(Status::className(), ['id' => 'status']);
}
/**
* get role name
*
*/
public function getStatusName()
{
return $this->status->status_name;
}
/**
* get list of roles for dropdown
*/
public static function getStatusList()
{
$droptions = Status::find()->asArray()->all();
return Arrayhelper::map($droptions, 'id', 'status_name');
}
}