T
T
Timur Khudiyev2020-06-19 12:51:45
Yii
Timur Khudiyev, 2020-06-19 12:51:45

Yii2.Why can a table relationship cause a missing property error?

Good day.
Oddly enough, I have done this a million times already, but the day before yesterday something went wrong.
There are 2 tables: Lessons- list of lessons and WatchedLesson- viewed lessons. Contains 3 columns id, lesson_id, user_id.

And now I'm trying to link the lessons table with the viewed lessons table.
ModelLessons

public function getWatch(){
        return $this->hasOne(WatchedLesson::class, ['lesson_id', 'id']);
    }

The rest of the code was generated with Gii and there is nothing out of the ordinary.

The bottom line is that when you get a lesson
$model = \app\models\Lessons::find()->where(['id' => 8])->with('watch')->one();

crashes error
Getting unknown property: app\models\Lessons::lesson_id

But the joke is that lesson_idthis is a table field WachedLesson, and not Lessons
In theory , lesson_idI can get it like this: $model->watch->lesson_id

I tried to do both lazy and eager loading - the result is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Khudiyev, 2020-06-19
@t_khudiyev

The issue has been resolved. Thanks to everyone and especially @usdglander

return $this->hasOne(WatchedLesson::class, ['lesson_id' => 'id']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question