M
M
Maxim2018-06-18 17:13:50
Yii
Maxim, 2018-06-18 17:13:50

How to replace such code?

Hello! How can we replace such usages and move them from the controller to the model?

....
public function actionCreate()
    {
       $event_id = Registration::getEventId();//Обращение к модулю. В нем получаем event_id
        $model = new RegBattle(['event_id'=>$event_id])
///другое...
}
....

public function actionCreate()
   ....
public function actionCreate($event_id)
    {
        $model = new RegBattle(['event_id'=>$event_id]) //event_id получаем из get запроса
///другое...
}
....

Tried doing something like this:
/**
     * Инициализация модели
     */
    public function init()
    {
        parent::init();

        if ($this->isNewRecord){
            $this->event_id = Registration::getEventId();
            $this->number = self::setNumber();
        }
    }

But the code above is always initialized when the model is connected, but it is necessary only before creating a record, but not before saving! That is, to $this->number = self::setNumber();automatically initialize the value with the last number in the input field
/**
     * Добавить номер
     * @return mixed
     */
    public static function setNumber()
    {
        $event_id = Registration::getEventId();
        return self::find()->where(['event_id' => $event_id])->max('number') + 1;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OKyJIucT, 2018-06-18
@OKyJIucT

public function actionCreate($event_id = null)
{
  if($event_id) {
    $model = new RegBattle(['event_id'=>$event_id]) //event_id получаем из get запроса
  } else {
    // ... другое
  }
        

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question