H
H
hollanditkzn2017-03-22 11:48:25
Yii
hollanditkzn, 2017-03-22 11:48:25

Why is related tables not showing in DetailView?

I have related tables

public function getIdSotrud()
    {
        return $this->hasOne(User::className(), ['id' => 'id_sotrud']);
    }

And it is not possible to display name from related tables
in the controller
public function actionView($id)
    {
        $user->idSotrud->name;

        return $this->render('view', [
            'model' => $this->findModel($id),
            'user' => $user,
        ]);
    }

And in the widget itself
DetailView::widget([
 [
                'attribute' => 'id_sotrud',
                'value' => $user,
            ],
])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-03-22
@hollanditkzn

It is not displayed because you wrote nonsense. Before you write the code to the dock, you would read

public function actionView($id)
{
    $user->idSotrud->name; // что за переменная $user откуда она взялась? что Вы вообще хотите сделать в этой строке.
    return $this->render('view', [
        'model' => $this->findModel($id),
        'user' => $user,
    ]);
}

DetailView::widget([
    [
        'attribute' => 'id_sotrud', // а тут что Вы ожидаете увидеть? 
                                  // данные реляции? так Вы ж их не выводите
        'value' => $user,
    ],
])

M
Maxim Timofeev, 2017-03-22
@webinar

You would include errors, as it $user->idSotrud->name;would immediately give an error.

public function actionView($id)
    {
        $user->idSotrud->name; // тут же у Вас просто переменная из ниоткуда появилась

        return $this->render('view', [
            'model' => $this->findModel($id), //тут тоже
            'user' => $user,
        ]);
    }

something like this is expected:
public function actionView($id)
    {
        $model = MyModel::find()->andWhere(['id'=>$id])->with('idSotrud')->one();
        $sotrud_name = $model->idSotrud->name;

        return $this->render('view', [
            'model' => $model,
            'sotrud_name' => $sotrud_name,
        ]);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question