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

时间:2018-07-19 13:36:51

标签: join gridview 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#

出什么问题了?

3 个答案:

答案 0 :(得分:0)

  1. 创建搜索类别

    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. 搜索类别添加到操作

    public function actionIndex()
    {
        $search = new sSearch()
        return $this->render('index', [
            'dataProvider' => $search->search(),
        ]);
    }
    

答案 1 :(得分:0)

  • 有很多方法可以做到这一点。

方法1:

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'],
    ],
]); ?>

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

答案 2 :(得分:0)

首先,您应该检查您的关系: 应该是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'],
    ],
]); ?>
相关问题