yii2 合并两个数据提供者并在网格中显示它们

问题描述 投票:0回答:2

我正在尝试合并网格中具有不同字段的两个表,但出现错误,就像我无法调用第二个第一个表字段

桌子

MovimentiCredito |ID|IDTessera|IDTipoMovCredito|IDUTente|数据|MovCredito|注释| 蓬蒂运动 |ID|IDTessera|IDTipoMovPunti|IDUTente|数据|MovPunti|注释|

我的代码

 $MCdataprovider= new ActiveDataProvider([
 'query' => MovimentiCredito::find()->where(['IDTessera'=>$card->ID])->orderBy('data DESC'),
 'pagination' => [
 'pageSize' => 20,
  ],
 ]); 
 $MPdataprovider= new ActiveDataProvider([
'query' => MovimentiPunti::find()->where(['IDTessera'=>$card->ID])->orderBy('data DESC'),
'pagination' => [
'pageSize' => 20,
     ],
 ]); 
 $datamov = array_replace_recursive ( $MPdataprovider->getModels(),$MCdataprovider->getModels());
 $dataProvider_all = new ArrayDataProvider([
   'allModels' => $datamov
 ]);

网格

<?php Pjax::begin(['id' => 'Movimenti2']) ?>        
  <?php Print_r($dataProvider_all);?>
          <?php echo GridView::widget([
    'dataProvider' => $dataProvider_all,
          'columns' => [
        [
            'attribute' => 'Data',
            'format' => 'text'
        ],
              ['attribute' => 'MovPunti',
            'format' => 'text'],
 ['attribute' => 'MovCredito',
            'format' => 'text'],
                 
                   ['attribute' => 'Note',
            'format' => 'text'],
            [
            'label' => 'Cancella',
            //'attribute' => 'education',
            //'filter' => ['0' => 'Elementary', '1' => 'Secondary', '2' => 'Higher'],
            //'filterInputOptions' => ['prompt' => 'All educations', 'class' => 'form-control', 'id' => null]
                 'format' => 'raw',
            'value' => function($data) {

                        return Html::a('modifica',['movimenti/delete','id' => $data['ID'] ]);
            }
    
        ],
              ]
]);
  ?>   

        
        
          <?php Pjax::end() ?>

错误

未知属性 – yii ase\UnknownPropertyException

获取未知属性:app\models\MovimentiCredito::MovPunti

如果我改变 $datamov = array_replace_recursive ( $MPdataprovider->getModels(),$MCdataprovider->getModels());

数据提供者的顺序,如果 Punti 是第一个,我会得到不同的错误,我会在 Credito 字段上得到一个错误,反之亦然,但如果我打印组合的数据提供者充满了数据...... 我该如何解决? 感谢您的帮助!

php datagridview yii2 dataprovider cactivedataprovider
2个回答
0
投票

您需要使用此处所示的联合方法https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder因此根据其中的代码,您的可能会是像这样的东西

$query1 = (new \yii\db\Query())
    ->select("ID, IDTessera, IDTipoMovCredito as IDTipoMov, IDUtente, Data, MovCredito as mov, Note")
    ->from('MovimentiCredito');

$query2 = (new \yii\db\Query())
    ->select('ID, IDTessera, IDTipoMovPunti as IDTipoMov, IDUtente, Data, MovPunti as mov, Note')
    ->from('MovimentiPunti');

$query1->union($query2);

$dataProvider_all = new ActiveDataProvider([
    'query' => $query1,
    'pagination' => [
        'pageSize' => 20,
    ],
]); 

0
投票

用这段代码解决了

   $MCdataprovider= new ArrayDataProvider ([
    'allModels' => MovimentiCredito::find()->where(['IDTessera'=>$card->ID])->asArray()->all(),
    'pagination' => [
        'pageSize' => count(MovimentiCredito::find()->where(['IDTessera'=>$card->ID])->asArray()->all()),
    ],
]); 
     $MPdataprovider= new ArrayDataProvider ([
    'allModels' => MovimentiPunti::find()->where(['IDTessera'=>$card->ID])->asArray()->all(),
    'pagination' => [
        'pageSize' => count(MovimentiPunti::find()->where(['IDTessera'=>$card->ID])->asArray()->all()),
    ],
]); 
 $datamov = array_replace_recursive ($MCdataprovider->getModels(),$MPdataprovider->getModels()); //$MPdataprovider->getModels(),$MCdataprovider->getModels());

$dataProvider_all = new ArrayDataProvider([
  'key'=>'ID',
    'allModels' => $datamov,
        'sort' => [
            'attributes' => [ 'Data' ],
        ],
]);

原因是使用arraydataprovider时需要使用key来合并 谢谢大家的支持!

© www.soinside.com 2019 - 2024. All rights reserved.