Jump to content

Both fields update automatically?


sadboys

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,
]);

}
}
Link to comment
Share on other sites

All of those object manipulations may mean something to you, but they don't tell us a thing.

 

What kind of debugging have you done to see what's happening when you do this?  That will be the solution for you.  Do some echos 

 

I did some var dumps and still when I update it still updates and gets the current date and time.

 

In actionCreate

 

When I create something it gets the current date and time and it fills the 

$model->time_start = date('y-m-d h:i:s');

In order to fill in the time_end I need to update what I have created first which was in actionUpdate I added this

$model->time_start = date('y-m-d h:i:s');
Link to comment
Share on other sites

what's your table schema? Where's the query that you're running?

 

If it's MySQL, this is the first thing that came to mind, for me:

 

An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value.

Link to comment
Share on other sites

https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).

Link to comment
Share on other sites

Archived

This topic is now archived and is 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.