简体   繁体   English

如何从 model A 在视图 B Yii2 中获取数据

[英]How to get data from model A in view B Yii2

I am quite new to Yii 2 and I am currently trying to display data from the Teams model in the User view in a GridView .我对 Yii 2 很陌生,我目前正在尝试在 GridView 的User视图中显示来自Teams GridView的数据。 In Yii1 I used a function with the $data variable.在 Yii1 中,我使用了带有$data变量的 function。 In Yii2 it does not seem to work that way.在 Yii2 中,它似乎不是这样工作的。

I have tried the following in the /user/index.php我在/user/index.php中尝试了以下内容

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Users');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a(Yii::t('app', 'Create User'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            // ['class' => 'yii\grid\SerialColumn'],
            // 'iduser',
            'username',
            'surname',
            'givenname',
            'email:email',
            'mobile',
            'admin:boolean',
            'language',
            // Below is what I have been trying to do so far...
            ['class' => 'yii\grid\DataColumn',
             'label' => 'Teams',
             'value' => 'getTeams($data-teams)'
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>


</div>

<?php
// And this is the function I use. In Yii1 it worked just fine.
function getTeams($data) {
    $tmp = "";foreach ($data as $team) {
      $tmp .=$team['name'].", ";
    }
    return trim($tmp, ", ");
  }
?>

I have been looking around but can't seem to find a solution for this one.我一直在环顾四周,但似乎无法找到解决方案。 Basically I simply want the Team to be displayed in which the user is a part of.基本上我只是希望显示用户所在的团队。 There is a table called UserHasTeam in which the id's of the teams and users are saved.有一个名为UserHasTeam的表,其中保存了团队和用户的 ID。

I am a little lost on how to go about the entire thing.我对如何 go 关于整个事情有点迷茫。 Any help is greatly appreciated.任何帮助是极大的赞赏。

I assume you have properly set relation to Team model in User model.我假设您在用户 model 中正确设置了与团队 model 的关系。 See the Relations via Junction Table part of guide if you don't know how to set the relation.如果您不知道如何设置关系,请参阅指南的通过连接表的关系部分。

You can use closure in $value property of DataColumn to control what value is used for column.您可以在DataColumn$value属性中使用闭包来控制用于列的值。

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        // ... other columns ...
        [
            'label' => 'Teams',
            'value' => function($model) {
                $tmp = "";
                foreach ($model->teams as $team) {
                    $tmp .= $team->name .", ";
                }
                return trim($tmp, ", ");
            }
        ],
    ],
]); ?>

You might also want to consider using eager loading to load all related at once instead of running query for each user in grid.您可能还想考虑使用预先加载来一次加载所有相关的,而不是为网格中的每个用户运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM