php - Yii2 Use two Models in one GridView -


i have gridview displays employee's summary of his/her payslip. here's screenshot more understanding:

enter image description here

those highlighted columns come different model user model while rest come payslip model.

how merge 2 models in 1 gridview? because in gridview, more have single model display data. how 2 models?

here's code in payslip model, note getuser() generated gii since user_id foreign key in payslip table:

public function getuser() {     return $this->hasone(user::classname(), ['user_id' => 'user_id']); }  public function getfirstname() {     return $this->user ? $this->user->fname : 'first name'; }  public function getlastname() {     return $this->user ? $this->user->lname : 'last name'; } 

the payslip controller:

public function actionindex() {     $searchmodel = new payslipsearch();     $dataprovider = $searchmodel->search(yii::$app->request->queryparams);      return $this->render('index', [         'searchmodel' => $searchmodel,         'dataprovider' => $dataprovider,     ]); } 

and payslip view:

<?php        echo gridview::widget([         'dataprovider' => $dataprovider,         //'filtermodel' => $searchmodel,         'columns' => [             ['class' => 'yii\grid\serialcolumn'],              'payslip_id',             //'store_id',             'firstname',             'lastname',             'total_earnings',             'total_deduction',             'net_pay',              ['class' => 'yii\grid\actioncolumn'],         ],     ]);  ?> 

btw, in example, created payslip #1 manually give demo.

additional info: logged in user bizadmin user, , of bizadmin's staff users should displayed in payslip table (that table above) if these staff users still don't have payslip created them. default, table occupied staff users under logged in bizadmin user, , staff users still have no payslips created indicated "create payslip"

here's example in kashflow: enter image description here

update view to:

<?php        echo gridview::widget([         'dataprovider' => $dataprovider,         //'filtermodel' => $searchmodel,         'columns' => [             ['class' => 'yii\grid\serialcolumn'],              'payslip_id',             //'store_id',             ['value' => function ($data) {return $data->getfirstname();}, 'label' => 'first name'],             ['value' => function ($data) {return $data->getlastname();}, 'label' => 'lastname'],              'total_earnings',             'total_deduction',             'net_pay',              ['class' => 'yii\grid\actioncolumn'],         ],     ]);  ?> 

and in $searchmodel->search add relation, that:

 $query = payslip::find()->with(['user']); 

read data column - http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#data-column


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -