0

i'm trying to merge two table with different field in a grid but i get an error like i cant call second fist table fields

the tables

MovimentiCredito |ID|IDTessera|IDTipoMovCredito|IDUtente|Data|MovCredito|Note| MovimentiPunti |ID|IDTessera|IDTipoMovPunti|IDUtente|Data|MovPunti|Note|

My Code

 $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
 ]);

The Grid

<?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() ?>

the error

Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\MovimentiCredito::MovPunti

if i change in $datamov = array_replace_recursive ( $MPdataprovider->getModels(),$MCdataprovider->getModels());

the order of the dataprovider i get different error if Punti is first i get an error on Credito Field and viceversa, but if i print the combined dataprovider is full of data.... How can i solve? Thanks for the help!

  • 1
    If you really need to have two ActiveData sources in one grid make a union of them so there will be one source. – Bizley Aug 04 '20 at 09:06
  • tks for the answer but i need to use activedata, is there a way to use it? – marco cardinale Aug 04 '20 at 10:50
  • I'm not sure how to do it with ActiveDataProvider but I cannot think of any reason why it's required here. SqlDataProvider with [union](https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#union) would be fine. – Bizley Aug 04 '20 at 11:02

2 Answers2

0

You need to use the union method as seen here https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder so based on the code in there, yours is probably going to be something like this

$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,
    ],
]); 
Mike
  • 182
  • 1
  • 3
  • 15
0

Solved with this code

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

the reason was that when you use arraydataprovider you need to use a key to merge tks everybody for the support!