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

时间:2015-06-15 04:44:40

标签: view model yii2

我在模型中的方法中显示结果时遇到了一些麻烦。这是方法:

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

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

1 个答案:

答案 0 :(得分:0)

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

discountFee是模型的一个功能,在你放置模型之前它永远不会工作。你可以做一个

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