Load data to a form from a different model in yii2

2019-09-19 01:10发布

问题:

I am creating a form to insert data into table salary. The associated Model is Salary. in the form I have fields s_wageperday,s_conveyanceperday. I have a separate table ratechart where these values are already stored. There's only one row in the table ratechart and it should be.the columns are - wage_per_day, conveyance_per_day. What I want is - when I load the Create form for salary - It should load the data from ratechart table in the respective textfield. What I've done - In RatechartController I've added the following code

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);
    }

As far I know this data can be pulled to the form using Javascript in salary _form Javascriptcode I've written so far (which is probably wrong) -

<?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);
?>

Please help.

Model Salary

<?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',
        ];
    }
}

Model Ratechart

<?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',
        ];
    }
}

I've tried the following code in my _form

<?= $form->field('s_ovtrateperhour', Ratechart::findOne(1)->ovt_per_hour,['showLabels'=>false])->textInput(['placeholder'=>'Overtime rate per hour']) ?>

But getting error - Call to a member function formName() on string

标签: yii2