2

Вывожу данные через виджет

$dataProvider = new ActiveDataProvider([
    'query' => \common\models\Pay::find(),
    'pagination' => [
        'pageSize' => 10,
    ],
]);
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'attribute' => 'data',
            'format' => ['date', 'php:d.m.Y']
        ],
        'user.email',
        [
            'class' => 'yii\grid\ActionColumn',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return '<a href="/admin/pay/delete/'.$key.'" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>';
                },
                'update' => function ($url, $model, $key) {
                    return '<a href="/admin/pay/edit/'.$key.'" class="btn btn-info"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>';
                },
                'view' => function ($url, $model, $key) {
                    return '';
                },
            ]
        ]
    ],
]);

в таблице Pay есть поле user_id, которое связано с таблицей User. Поэтому в columns я пишу

'user.email',

но мне выдает ошибку, что в Pay нет поля user. Если я напишу

'user_id.email'

ничего не выводит. Помогите разобраться

2
  • а в модели Pay есть релейшен getUser?
    – Blacknife
    13 мая 2017 в 9:41
  • уже есть) спасибо, как раз нашел решение
    – Diefair
    13 мая 2017 в 9:59

1 ответ 1

1

Надо добавить метод

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

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.