2015-06-15 17 views
0

我在模型中显示我的结果时出现了一些问题。这是方法:如何从模型中的方法中获取值?

/** 
* @return \yii\db\ActiveQuery 
*/ 
public function getSchoolFee() 
{ 
    return $this->hasOne(SibuSchool::className(), ['school_fee_id' => 'school_fee_id']); 
} 

/** 
* @return \yii\db\ActiveQuery 
*/ 
public function getScholarshipFee() 
{ 
    return $this->hasOne(SibuScholarship::className(),['scholarship_id'=>'scholarship_id']); 
} 

/** 
* @return \yii\db\ActiveQuery 
*/ 
public function getDiscountFee() 
{ 
    $scholarship= $this->getScholarshipFee(); 
    $school= $this->getSchoolFee(); 
    $data= new SibuPayment(); 
    $data->total_payment=$school->amount-($scholarship->school_discount*$school->amount); 
    return $data; 
} 

我想要显示getDiscountFee方法的结果。我使用此代码对我的观点:

<?php echo $this->render('_search3', ['model' => $searchModel]); ?> 
<?= 
GridView::widget([ 
    'dataProvider' => $dataProvider, 
    // 'filterModel' => $searchModel, 
    'columns' => [ 
     [ 
      // 'filterModel'=>'dataProviderSearch', 
      'attribute' => 'Nama', 
      'value' => 'virtual.student_name' 
     ], 
     [ 
      'attribute' => 'Uang Sekolah', 
      'value' => 'discountFee.total_payment' 
     ], 
     [ 
      'attribute' => 'Uang Kantin', 
      'value' => 'canteenFee.total_amount' 
     ], 
     [ 
      'attribute' => 'Uang Asrama', 
      'value' => 'dormitoryFee.amount' 
     ], 
     [ 
      'attribute' => 'Uang Perpustakaan', 
      'value' => 'libraryFee.amount' 
     ], 
     [ 
      'attribute' => 'Uang Les', 
      'value' => 'courseFee.amount' 
     ], 
     [ 
      'attribute' => 'Adm. Bank', 
      'value' => 'administration' 
     ], 
     [ 
      'attribute' => 'status', 
      'value' => function ($model) { 
       if ($model->scholarship_id == 0) { 
        return 'Tanpa Beasiswa'; 
       } else if ($model->scholarship_id == 1) { 
        return 'Beasiswa Ekonomi Kategori A 100%'; 
       } else if ($model->scholarship_id == 2) { 
        return 'Beasiswa Ekonomi Kategori B 50%'; 
       } else if ($model->scholarship_id == 3) { 
        return 'Beasiswa Prestasi Kategori 50%'; 
       } else if ($model->scholarship_id == 4) { 
        return 'Beasiswa Prestasi Kategori 25%'; 
       } else { 
        return 'Beasiswa Prestasi Kategori 10%'; 
       } 
      }, 
     ], 
     //'payment_class', 
     [ 
      'attribute' => 'payment_class', 
      'value' => function ($model) { 
       if ($model->payment_class == 0) { 
        return 'Tanpa Golongan'; 
       } else if ($model->payment_class == 1) { 
        return 'Golongan 1'; 
       } else if ($model->payment_class == 2) { 
        return 'Golongan 2'; 
       } else if ($model->payment_class == 3) { 
        return 'Golongan 3'; 
       } else if ($model->payment_class == 4) { 
        return 'Golongan 4'; 
       } 
      }, 
     ], 
     [ 
      'attribute' => 'Total', 
      'value' => 'total_payment' 
     ], 
     ['class' => 'yii\grid\ActionColumn', 
      'template' => '{update_r}{del}', 
      'buttons' => [ 
       'update_r' => function ($url, $model) { 
        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [ 
           'title' => Yii::t('yii', 'View_r'), 
        ]); 
       }, 
         'del' => function ($url, $model) { 
        return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [ 
           'title' => Yii::t('yii', 'Del'), 
        ]); 
       }, 
        ] 
       ], 
      ], 
     ]); 
     ?> 

它工作正常,如果我这样显示:

  [ 
      'attribute' => 'Uang Sekolah', 
      'value' => 'scholarshipFee.school_discount' 
     ], 

这:

  [ 
      'attribute' => 'Uang Sekolah', 
      'value' => 'schoolFee.amount' 
     ], 

任何人可以帮助我吗?如果你需要任何东西随意问。

回答

0
[ 
     'attribute' => 'Uang Sekolah', 
     'value' => 'discountFee.total_payment' 
    ], 

discountFee是一个模型的功能,它将永远不会工作,直到你把模型放在那里。你可以做一个

[ 
     'attribute' => 'Uang Sekolah', 
     'value' => function ($model) { 
       return $model->getDiscountFee()->total_payment; 
      } 
     }, 
    ], 
相关问题