【问题标题】:Q: yii2 dataProvider doesn't display join attributesQ: yii2 dataProvider不显示join属性
【发布时间】:2018-11-29 22:26:48
【问题描述】:

我在用户和论文之间有关系,我的 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,
    ]);
}

【问题讨论】:

  • 不确定你在这里问什么,但你应该使用-&gt;joinWith(['user', 'thesis th' =&gt; function (\yii\db\ActiveQuery $query) { $query-&gt;where(['th.staff'=&gt; Yii::$app-&gt;user-&gt;identity-&gt;id]); } ]),你可以在一次调用joinWith()时将多个关系指定为数组。
  • 还有,您在 gridview 中使用的是哪种搜索模型?
  • 我的 $searchModel 来自上面的 SearchRequest->search() ,无论如何它仍然不打印论文属性。现在,我的关系是 User-Student-Thesis 与 User-Student (0-1,1-1) 和 Student-Thesis (N-N),它们创建了一个名为 Request 的新表/模型。我想问题可能出在 Request 模型中,因为 joinWith() 使用了那些 get 方法。
  • 什么是请求模型?

标签: php gridview yii2 dataprovider


【解决方案1】:

您需要在模型上添加关系获取器。

在您的论文模型上,您需要添加类似

的内容
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
    ],
]); ?>

【讨论】:

  • 这解决了问题。我有一个 getUser() 函数,而不是一个 getUser0(),谢谢。你能告诉我为什么会这样吗?
  • 我相信 Model::getUser 不会覆盖 $user 属性的内置 getter,因此您需要将关系 getter 命名为其他名称
【解决方案2】:

最好在 $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]);
.
.
.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多