在yii2中将数据加载到来自不同模型的表单

时间:2016-06-28 04:27:02

标签: yii2

我正在创建一个表单以将数据插入表salary。相关的模型是薪水。在表格中我有字段s_wageperday,s_conveyanceperday。我有一个单独的表ratechart,其中已经存储了这些值。表速率图中只有一行,它应该是。列是 - wage_per_day, conveyance_per_day。我想要的是 - 当我加载工资的创建表单时 - 它应该从相应的文本字段中的ratechart表加载数据。 我做了什么 - 在RatechartController中,我添加了以下代码

public function actionGetForRatechart()
    {
        $rates = Employee::findOne(1)->asArray();
        //$bottle -> select(['productnames.productnames_productname','productnames.bottletype','bottlename.unitprice'])->from('Productnames')->leftJoin('bottlename','productnames.bottletype = bottlename.bottlename')->where(['productnames_productname'=>$catid])->limit(1);
        echo Json::encode($rates);
    }

据我所知,这些数据可以在salary _form中使用Javascript拉到表单中 我到目前为止写的Javascriptcode(可能是错的) -

<?php
$script = <<< JS
$('#rateid').change(function(){   
    var  = $(this).val();

     $.get('index.php?r=salary/ratechart/get-for-ratechart',{ 1 : 1 }, function(data){
        alert(data);
        var data = $.parseJSON(data);
        $('#salary-s_wageperday').attr('value',data.wage_per_day);
    });
});
JS;
$this->registerJs($script);
?>

请帮忙。

模型薪水

<?php

namespace frontend\modules\salary\models;

use Yii;

/**
 * This is the model class for table "salary".
 *
 * @property string $s_id
 * @property string $s_date
 * @property string $s_period
 * @property string $s_empid
 * @property string $s_empname
 * @property integer $s_workingdays
 * @property integer $s_leave
 * @property integer $s_holiday
 * @property integer $s_wageperday
 * @property integer $s_totalwage
 * @property integer $s_ovthour
 * @property integer $s_ovtrateperhour
 * @property integer $s_ovtamount
 * @property integer $s_tiffinworkingday
 * @property integer $s_tiffinrateperday
 * @property integer $s_wdtiffinamount
 * @property integer $s_sundayworked
 * @property integer $s_sundayrate
 * @property integer $s_sundaytiffinamount
 * @property integer $s_nightcount
 * @property integer $s_nightallrate
 * @property integer $s_nightallowance
 * @property integer $s_convday
 * @property integer $s_convrate
 * @property integer $s_conveyanceamount
 * @property integer $s_tiffinovtcount
 * @property integer $s_tiffinovtrate
 * @property integer $s_tiffinovtamount
 * @property integer $s_incentivecount
 * @property integer $s_incentiverate
 * @property integer $s_incentive
 * @property integer $s_totalearning
 * @property integer $s_epf
 * @property integer $s_esi
 * @property integer $s_ptax
 * @property integer $s_takehome
 */
class Salary extends \yii\db\ActiveRecord
{
    public $value;
    public $totaldays;
    public $ratechart;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'salary';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['s_date', 's_period', 's_empid', 's_empname','s_workingdays', 's_leave', 's_holiday','s_wageperday', 's_totalwage', 's_ovthour', 's_ovtrateperhour', 's_ovtamount', 's_tiffinworkingday', 's_tiffinrateperday', 's_wdtiffinamount', 's_sundayworked', 's_sundayrate', 's_sundaytiffinamount', 's_nightcount', 's_nightallrate', 's_nightallowance', 's_convday', 's_convrate', 's_conveyanceamount', 's_tiffinovtcount', 's_tiffinovtrate', 's_tiffinovtamount', 's_incentivecount', 's_incentiverate', 's_incentive', 's_totalearning', 's_epf', 's_esi', 's_ptax', 's_takehome'], 'required'],
            [['s_date','value','totaldays'], 'safe'],
            [['s_workingdays', 's_leave', 's_holiday', 's_wageperday', 's_totalwage', 's_ovthour', 's_ovtrateperhour', 's_ovtamount', 's_tiffinworkingday', 's_tiffinrateperday', 's_wdtiffinamount', 's_sundayworked', 's_sundayrate', 's_sundaytiffinamount', 's_nightcount', 's_nightallrate', 's_nightallowance', 's_convday', 's_convrate', 's_conveyanceamount', 's_tiffinovtcount', 's_tiffinovtrate', 's_tiffinovtamount', 's_incentivecount', 's_incentiverate', 's_incentive', 's_totalearning', 's_epf', 's_esi', 's_ptax', 's_takehome'], 'integer'],
            [['s_period'], 'string', 'max' => 15],
            [['s_empid'], 'string', 'max' => 20],
            [['s_empname'], 'string', 'max' => 70],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            's_id' => 'ID',
            's_date' => 'Date',
            's_period' => 'Period',
            's_empid' => 'Empid',
            's_empname' => 'Empname',
            's_workingdays' => 'Workingdays',
            's_leave' => 'Leave',
            's_holiday' => 'Holiday',
            's_wageperday' => 'Wageperday',
            's_totalwage' => 'Totalwage',
            's_ovthour' => 'Ovthour',
            's_ovtrateperhour' => 'Ovtrateperhour',
            's_ovtamount' => 'Ovtamount',
            's_tiffinworkingday' => 'Tiffinworkingday',
            's_tiffinrateperday' => 'Tiffinrateperday',
            's_wdtiffinamount' => 'Wdtiffinamount',
            's_sundayworked' => 'Sundayworked',
            's_sundayrate' => 'Sundayrate',
            's_sundaytiffinamount' => 'Sundaytiffinamount',
            's_nightcount' => 'Nightcount',
            's_nightallrate' => 'Nightallrate',
            's_nightallowance' => 'Nightallowance',
            's_convday' => 'Convday',
            's_convrate' => 'Convrate',
            's_conveyanceamount' => 'Conveyanceamount',
            's_tiffinovtcount' => 'Tiffinovtcount',
            's_tiffinovtrate' => 'Tiffinovtrate',
            's_tiffinovtamount' => 'Tiffinovtamount',
            's_incentivecount' => 'Incentivecount',
            's_incentiverate' => 'Incentiverate',
            's_incentive' => 'Incentive',
            's_totalearning' => 'Totalearning',
            's_epf' => 'EPF',
            's_esi' => 'ESI',
            's_ptax' => 'PTax',
            's_takehome' => 'Takehome',
        ];
    }
}

模型费率图

<?php

namespace frontend\modules\salary\models;

use Yii;

/**
 * This is the model class for table "ratechart".
 *
 * @property integer $rc_id
 * @property integer $ovt_per_hour
 * @property integer $tiffin_per_working_day
 * @property integer $conveyance_per_working_day
 * @property integer $tiffin_per_ovt_day
 * @property integer $tiffin_per_sunday
 * @property integer $night_allowance_per_night
 * @property integer $incentive_per_count
 */
class Ratechart extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ratechart';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['rc_id'], 'required'],
            [['rc_id', 'ovt_per_hour', 'tiffin_per_working_day', 'conveyance_per_working_day', 'tiffin_per_ovt_day', 'tiffin_per_sunday', 'night_allowance_per_night', 'incentive_per_count'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'rc_id' => 'Rc ID',
            'ovt_per_hour' => 'Ovt Per Hour',
            'tiffin_per_working_day' => 'Tiffin Per Working Day',
            'conveyance_per_working_day' => 'Conveyance Per Working Day',
            'tiffin_per_ovt_day' => 'Tiffin Per Ovt Day',
            'tiffin_per_sunday' => 'Tiffin Per Sunday',
            'night_allowance_per_night' => 'Night Allowance Per Night',
            'incentive_per_count' => 'Incentive Per Count',
        ];
    }
}

我在_form

中尝试了以下代码
<?= $form->field('s_ovtrateperhour', Ratechart::findOne(1)->ovt_per_hour,['showLabels'=>false])->textInput(['placeholder'=>'Overtime rate per hour']) ?>

但是收到错误 - Call to a member function formName() on string

0 个答案:

没有答案