yii2 如何连接两个表并在 gridview 中显示?

How to join two tables and show in gridview in yii2?

我有三张桌子? s(s#,firstName,lastName)p(p#,name)sp(s#,p#,qty)。 s模型(由gii创建)中有所有关系。

代码:

public function actionIndex()
    {


        $dataProvider = new ActiveDataProvider([
            'query' =>s::find(),

        ]);

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

在索引视图中:

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


        'firstName',
        'lastName',

        [
         'attribute'=>'qty',
         'value'=>'sp.qty'

        ]

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

我想在网格视图中显示数量。但它没有显示。 我想在 gridview 中显示以下查询:

select firstName,lastName,qty from s join sp on s.s#=sp.s#

有什么问题?

  1. 创建搜索class

    class sSearch extend s {
        public $name;
        public $qnt;
        public functioin search()
        {
            return new ActiveDataProvider([
                'query' =>self::find()
                   ->select([
                     's.*',
                     'sp.qnt'
                   ])
                   ->innerJoin('sp','s.s = sp.s'),
    
            ]);
        }
    }
    
  2. 搜索class添加到操作

    public function actionIndex()
    {
        $search = new sSearch()
        return $this->render('index', [
            'dataProvider' => $search->search(),
        ]);
    }
    
  • 有很多方法可以做到这一点。

方法一:

public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' =>p::find(),

        ]);

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

在您的网格视图中:

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

        [
            //to be declared in PSearch Model
            'attribute' => 'first_name',
            'value' => function ($data) {
                // Relation will be setup in the model
                if ($data->sp) {
                    return $data->sp->s->first_name;
                } 
                return 'Display whatever is required';
            },
        ],

        [
            //to be declared in PSearch Model
            'attribute' => 'last_name',
            'value' => function ($data) {
                // Relation will be setup in the model
                if ($data->sp) {
                    return $data->sp->s->last_name;
                } 
                return 'Display whatever is required';
            },
        ],

        'name',


        [
            //to be declared in PSearch Model
            'attribute' => 'quantity',
            'value' => function ($data) {
                // Relation will be setup in the model
                if ($data->sp) {
                    return $data->sp->quantity;
                } 
                return 'Display whatever is required';
            },
        ],

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

如果这没有帮助,请发表评论。谢谢!!

首先你应该检查你的关系: 应该是 hasOne 而不是 hasMany

getSps() {
 return $this->hasOne(Sp::className(), ['sId' => 'id']); 
}

并且您必须要求与 sps 建立关系,而不仅仅是 sp 因为您的关系名称是 getSps

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


        'firstName',
        'lastName',

        [
         'attribute'=>'qty',
         'value'=>'sps.qty'

        ]

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