I have two tables.
students(id,email,password,f_name,l_name,unique_id,create_date,last_update)
comments(id,ad_id,commented_user,comment,create_date,last_update)
table relation -> comments.commented_user = students.email
comments class
class Comments extends \yii\db\ActiveRecord
{
//public $commented_user_fName;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'comments';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['comment'], 'string'],
[['create_date', 'last_update'], 'safe'],
[['ad_id', 'commented_user'], 'string', 'max' => 64],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'ad_id' => 'Ad ID',
'commented_user' => 'Commented User',
'comment' => 'Comment',
'create_date' => 'Create Date',
'last_update' => 'Last Update',
];
}
public function beforeSave($insert) {
if ($this->isNewRecord){
$this->commented_user = $_SESSION['login_student_email'];
$this->create_date = new Expression('NOW()');
}
return parent::beforeSave($insert);
}
public function getStudentName(){
$this->hasOne(Students::className() ,['commented_user' => 'email']);
}
}
My comment Controller Index methord
public function actionIndex($id)
{
$model = new Comments();
$model->setAttribute('ad_id',$id);
$searchModel = new CommentsSearch();
$dataProvider = $searchModel->search(["CommentsSearch"=>['ad_id'=>$id]]);
return $this->renderAjax('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
I try to get f_name of student table in my ListView
<?=
ListView::widget([
'dataProvider' => $dataProvider,
'layout' => "{items}",
'itemView' => 'view',
]);
?>
Item view file code below
<a href="#"><?= $model->studentName->f_name ?></a>
I got this error
PHP Notice – yii\base\ErrorException Trying to get property of non-object
on this line <a href="#"><?= $model->studentName->f_name ?></a>
What is wrong with my code. Please help me