第十三

场景(scenario)的使用

例如:有三个场景,分别为创建,更新,确认回款

首先,定义所有的场景,及规则,如下所示:

<?php

namespace core\models;

use common\models\Base;
use Yii;
use yii\base\Exception;

class WmsSamplingApplication extends \common\models\WmsSamplingApplication
{

    const TYPE_HERB = 1;
    const TYPE_PRODUCT = 2;
    const CHECK_STATUS_INIT = 0;
    const CHECK_STATUS_OK = 1;
    const CHECK_STATUS_DENY = 2;

    public $disabled = false;

    public function scenarios()
    {
        $scenario = parent::scenarios();

        $scenario['scenario_create'] = [
                'wms_sampling_application_code',
                'common_producer_info_id',
                'wms_sampling_application_type',
                'wms_sampling_application_category',
                'wms_sampling_application_user_id',
                'wms_sampling_application_apply_at',
                'wms_sampling_application_apply_text'
            ];
        $scenario['scenario_update']=[
                'wms_sampling_application_code',
                'common_producer_info_id',
                'wms_sampling_application_type',
                'wms_sampling_application_category',
                'wms_sampling_application_user_id',
                'wms_sampling_application_apply_at',
                'wms_sampling_application_apply_text'
            ];
        $scenario['scenario_cash']=[
                'wms_sampling_application_cash_user_id',
                'wms_sampling_application_cash_at',
                'wms_sampling_application_cash_text',
                'wms_sampling_application_cash_status'
            ];
        return $scenario;
    }

    public function rules()
    {
        return [
            [['wms_sampling_application_code'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['common_producer_info_id'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_type'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_category'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_user_id'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_apply_at'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_apply_text'], 'required', 'message'=>'必填字段', 'on'=>['scenario_create', 'scenario_update']],

            [['wms_sampling_application_cash_user_id'], 'required', 'message'=>'必填字段', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_at'], 'required', 'message'=>'必填字段', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_text'], 'required', 'message'=>'必填字段', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_status'], 'required', 'message'=>'必填字段', 'on'=>['scenario_cash']],

            [['wms_sampling_application_code'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_type'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['common_producer_info_id'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['common_producer_info_name'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_user_id'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_user_name'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_user_phone'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_apply_at'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_apply_text'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_user_id'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_user_name'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_user_phone'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_at'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_status'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_check_text'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['created_at'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['updated_at'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['is_del'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_related_id'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_related_sheet_number'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_category'], 'integer', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_category_name'], 'string', 'on'=>['scenario_create', 'scenario_update']],
            [['wms_sampling_application_cash_balance'], 'string', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_user_id'], 'integer', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_user_name'], 'string', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_at'], 'string', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_text'], 'string', 'on'=>['scenario_cash']],
            [['wms_sampling_application_cash_status'], 'integer', 'on'=>['scenario_cash']],
        ];
    }

最后,使用场景 ,如下图所示:

public function actionCash($id)
    {
        $model = $this->findModel($id);
        $model->setScenario('scenario_cash');
        if ($model->load(Yii::$app->request->post())) {
            $userModel = \core\models\User::getUser(\Yii::$app->user->id);
            $model->wms_sampling_application_cash_user_name = $userModel->name;
            $model->wms_sampling_application_cash_at = strval(strtotime($model->wms_sampling_application_cash_at));
            $model->wms_sampling_application_cash_status = 1;

            $model->save(false);
            return $this->redirect(['index']);
        } else {
            $model->wms_sampling_application_cash_user_id = \Yii::$app->user->id;
            $model->wms_sampling_application_cash_at = time();
            return $this->renderAjax('cash', [
                'model' => $model,
            ]);
        }
    }

  

05-08 08:17