Q: yii2 dataProvider не отображает атрибуты соединения - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть связь между User и Thesis, и мой dataProvider просто отображает атрибуты пользователя, а не тезисы в Gridview.Есть ли лучший способ печати атрибутов соединения, чем: ['attribute'] => ['table.attribute']?

модель:

public function search($params)
{
    $query = Request::find()->joinWith('user')->joinWith('thesis')->where(['thesis.staff'=> Yii::$app->user->identity->id]);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

представление:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        //'id',
        'title',
        ['attribute' => 'thesis',
        'value' => 'thesis.title'],
        //'company',
        'description:ntext',
        'duration',
        ['attribute' => 'user'
        'value' => 'user.id'],
        //'requirements',
        //'course',
        'n_person',
        'ref_person',
        'is_visible:boolean',
        //'created_at',
        //'staff',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

контроллер:

public function actionIndex()
{
    $searchModel = new SearchRequest();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Ответы [ 2 ]

0 голосов
/ 12 декабря 2018

Вам необходимо добавить средства получения отношений в вашей модели.

В вашей модели диссертации необходимо добавить что-то вроде

public function getUser0 () {
    return $this->hasOne(\your\user\model\location\User::className(), ['id' => 'user']);
}

Наличие этого в вашей модели будет заполняться через ленивыйзагрузить отношение пользователя при вызове Thesis :: user0, в вашем случае что-то вроде этого:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        /// ---- other attributes
        ['attribute' => 'user0.name'],
        /// ---- other attributes
    ],
]); ?>
0 голосов
/ 04 декабря 2018

Лучше добавить свой код после $ this-> load ($ params);

Мой пример:

.
.
.
$this->load($params);
if (!$this->validate()) {
    // uncomment the following line if you do not want to return any records when validation fails
    // $query->where('0=1');
    return $dataProvider;
}
$query->joinWith('user');
$query->joinWith('thesis');
$query->andFilterWhere(['thesis.staff'=> Yii::$app->user->identity->id]);
.
.
.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...