如何从模型中的方法获取值?

How can I get the value from method in my model?

我在显示模型中某个方法的结果时遇到了一些问题。这是方法:

/**
 * @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'
        ],

有人可以帮助我吗?如果您有任何需要,请随时提出。

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

discountFee 是一个模型的函数,除非你把模型也放在那里,否则它永远不会起作用。你可以做一个

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