-1

I have two tables.

  1. students(id,email,password,f_name,l_name,unique_id,create_date,last_update)

  2. 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

2 Answers2

0

. is concatenation operator, so $model->studentName.f_name is not treated as $model->{studentName.f_name} but as {$model->studentName} . f_name.

You probably need to use $model->studentName->f_name.

rob006
  • 21,383
  • 5
  • 53
  • 74
0

You should access to the property as $model->studentName->f_name as with any other Model

G. Celarie
  • 39
  • 4
  • "= $model->studentName->f_name ?> " i tried that. I got this error on same line PHP Notice – yii\base\ErrorException Trying to get property of non-object – Madushanka Sampath Apr 03 '18 at 21:15
  • That would happen if the studentName relation is returning a null value. It may have something to do with how the relation is declared. Try changing the relation to: public function getStudentName(){ $this->hasOne(Students::className() ,['email' => 'commented_user']); } – G. Celarie Apr 03 '18 at 21:20