php - 如何向 gridview yii2 添加关系属性?

标签 php web yii yii2 frameworks

我有一个新问题 yii2。 如何在views/viewname/index中显示gridview中其他表的关系值,并为其添加一个确认按钮?

谢谢

<?php

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

/* @var $this yii\web\View */
/* @var $searchModel app\models\LaptopSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Laptops';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="laptop-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Laptop', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'network',
            'technology',
            'sup_id',
            'speaker',
            // 'optical_drive',
            // 'webcam',
            // 'touchpad',
            // 'card_reader',
            // 'ethernet',
            // 'vga',
            // 'hdmi',
            // 'usb3_ports',
            // 'usb2_ports',
            // 'usb_type_c',
            // 'thunderbolt_ports',
            // 'serial_ports',

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

如何在这里添加新属性并添加按钮?

最佳答案

为了获取可以添加到模型中的相关值

关系函数

public function getYourRelatedModel()
{
    return $this->hasOne(YourRelatedModel::className(), ['id' => 'your_id_fk']);
}

并为您需要的字段添加 setter/getter

public function getYour_field() {
    return $this->yourRelatedModel->your_field;
}

最后将列添加到 GridView

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

        'id',
        'network',
        'technology',
        'sup_id',
        'speaker',
        'your_field',

关于php - 如何向 gridview yii2 添加关系属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385376/

相关文章:

php - 选择多个选项 sql 和 php

c# - 使用 C# 和 HTTPrequest 登录网站

javascript - 站点搜索并在单独的网页中返回结果

rest - 以 RESTful 方式运行时如何使用行为?

php - 向 GridView 小部件添加过滤器

php - SQL/PHP - Between Query with time() 函数

PHP 数字清理正则表达式

javascript - AJAX 调用后提醒旧 session 数据

php - 如何为两个变量编写 SQL 语句,一个变量携带表名,另一个变量携带表中的特定列?

css - 分页:上一个/下一个链接对齐