My ultimate solution for scenarios

Hello guys,

I had much difficulty creating multiple scenarios for my models, mainly due to the conflict and change.

Place here my solution to resolve these conflicts.

I have grouped the creation of scenarios in two methods in my models.

getCustomScenarios = to create scenarios or manipule my scenarios

RemoveOfRequired = I understand that the scenario is all I want to work on my base, but all is not required,

Rules = In my rules, I get RemoveOfRequired, to list the required columns, modifying only the getCustomScenarios and RemoveOfRequired when you need.




<?php

namespace  common\models;


use Yii;

use yii\base\Model;


class  Mymodel extends  Model

{

  const SCENARIO_CREATE = 'criar';

  const SCENARIO_EDIT = 'editar';


  //manipulate scenarios

  public function getCustomScenarios()

  {

    return [

        self::SCENARIO_CREATE => ['title','description','state','date_creation'],

        self::SCENARIO_EDIT =>  ['title','description','state','date_update'],

    ];

  }


  // for aplicate in rulers

  // in my code i dont wont in scenario_edit daate_update required 

  public function RemoveOfRequired()

  {


    $allscenarios = $this->getCustomScenarios();


    $allscenarios[self::SCENARIO_EDIT] = array_diff($allscenarios[self::SCENARIO_EDIT], ['date_update']);


    return $allscenarios;


  }

  

  //set scenarios

  public function scenarios()

  {

    //$scenarios = parent::scenarios();

      $scenarios = $this->getCustomScenarios();

      return $scenarios;

  }


    //in my rulers

    public function rules()

    {

      $allscenarios = $this->RemoveOfRequired();

      

        return [

          [$allscenarios[self::SCENARIO_CREATE], 'required', 'on' => self::SCENARIO_CREATE],

          [$allscenarios[self::SCENARIO_EDIT], 'required', 'on' => self::SCENARIO_EDIT],

        ];

    }

}



In my controller I use the list to save the records the way I want.

“ace in the hole”, Apply the setting and the columns you want to save, using a single list.




<?php


namespace  app\controllers;

use app\models;

use Yii;


/**

 * 

 */

class Controller extends Controller

{


    //for create

    public function actionIndex()

    {

        $model = new Mymodel(['scenario'=>Mymodel::SCENARIO_CREATE]);


        if ($model->load(Yii::$app->request->post())){

          $modelScenarios = $model->getCustomScenarios();

          // i to use my scenario for to save im my database

          if($model->save(true, $modelScenarios[$model->scenario])){

            // return true

          }

          

        }

      

        return $this->render('index');

    }

    // for edit

    public function actionEdit($id)

    {

        $model = new Mymodel::findOne($id);

        $model->scenario = Mymodel::SCENARIO_EDIT

        if ($model->load(Yii::$app->request->post())){

          $modelScenarios = $model->getCustomScenarios();

          // i to use my scenario for to save im my database

          if($model->save(true, $modelScenarios[$model->scenario])){

            // return true

          }

          

        }

      

        return $this->render('index');

    }

}