Jump to content
sadboys

Yii2 - Both fields update

Recommended Posts

Hi everyone! In my Ticket _form.php. When I create ticket it gets the current time and fills in the time_start field automatically, in order to fill in the field time_end I need to update the ticket which also gets the current time when you update it. My problem is when I update it the time_start also updates and now they have the same value which supposed to be different.

 

I have this in my form:

<?= $form->field($model, 'time_start')->textInput(['readOnly' => true]) ?>


<?= $form->field($model, 'time_end')->textInput(['readOnly' => true]) ?>

Which automatically gets the current time of the system

 

In my TicketController.php

    public function actionCreate()
    {
        $model = new Ticket();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $model->time_start = date('y-m-d h:i:s');
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Ticket model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
                $model->time_end = date('y-m-d h:i:s');            
                return $this->render('update', [
                'model' => $model,
            ]); 

        }
    }

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.