Yii2网格视图语法可访问相关模型中的数据

时间:2018-07-19 22:55:10

标签: php yii2

例如,我可以使用以下命令在视图中检索数据:

<?=$model->instructor->manager->location['location_title']?>

我在Instructor model中定义了一个关系,例如:

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

/**
 * @return mixed
 */
public function getManager()
{
    return $this->hasOne(Manager::className(), ['user_id' => 'manager_id']);
}

Manager中的模型关系定义如下:

/**
 * @return mixed
 */
public function getLocation()
{
    return $this->hasOne(Location::className(), ['id' => 'location_id']);
}

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

现在,我如何在grid-view中检索相同的数据,同时还为相同的数据添加过滤器。

谢谢。

更新:Gridview代码

echo GridView::widget(
    [
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'id',
            'first_name',
            'last_name',
            //   'username',
            // 'auth_key',
            // 'password_hash',
            // 'password_reset_token',
            'email:email',

            // 'phone',

            //'user_role',
            ['attribute' => 'created_at', 'label' => 'Last Login', 'value' => function ($data) {
                return $data->getLast($data);
            },

                'contentOptions' => ['style' => 'width:100px']
            ],
            ['attribute' => 'created_at', 'label' => 'Create Date', 'contentOptions' => ['style' => 'width:100px'], 'value' => function ($data) {return date('M d, Y', $data->created_at);}

            ],
            // 'updated_at',

            ['attribute' => 'status', 'value' => function ($data) {
                return $data->getStatus($data);
            },

                'filter' => ['10' => 'Active', '0' => 'Deactive'],
                'contentOptions' => function ($data) {
                    $clr = $data->status == 10 ? 'green' : 'red';
                    return ['style' => 'width:80px;font-weight:bold;color:' . $clr];
                }

            ]
        ]
    ]
);

Update-1

public function actionInstructor()
{

    $cond = "user_role='instructor' ";
    $searchModel = new UserSearch();

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $cond);

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

更新2

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

1 个答案:

答案 0 :(得分:2)

您可以将关系指定为与.串联的关系名称字符串,如下所示。确保已在各个模型中定义了关系,以下是针对$dataProvider模型中的UserSearch的关系,并且User模型应具有定义的关系getInstructor()

 [
      'intructor.manager.location.location_title'
 ]

[
    'label' => 'Manager Location',
    'value' => function($model){
        return $model->instructor->manager->location->location_title;
    }
]

如果下面的$dataProvider模型具有UserSearch,应该是gridview代码。

<?=GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,

    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        //'id',
        'first_name',
        'last_name',
        [
         'label'=>'Manager Location'
         'attribute'=>'manager_id'.
         'value'=>function($model){
          return $model->instructor->manager->location->location_title;
         }
        ]
        //   'username',
        // 'auth_key',
        // 'password_hash',
        // 'password_reset_token',
        'email:email',

        // 'phone',

        //'user_role',
        ['attribute' => 'created_at', 'label' => 'Last Login', 'value' => function ($data) {
            return $data->getLast($data);
        },

            'contentOptions' => ['style' => 'width:100px'],
        ],
        ['attribute' => 'created_at', 'label' => 'Create Date', 'contentOptions' => ['style' => 'width:100px'], 'value' => function ($data) {return date('M d, Y', $data->created_at);}],
        // 'updated_at',

        ['attribute' => 'status', 'value' => function ($data) {
            return $data->getStatus($data);
        },

            'filter' => ['10' => 'Active', '0' => 'Deactive'],
            'contentOptions' => function ($data) {
                $clr = $data->status == 10 ? 'green' : 'red';
                return ['style' => 'width:80px;font-weight:bold;color:' . $clr];
            },
        ],