Jump to content

Both fields update automatically?


sadboys
Go to solution Solved by Sepodati,

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');
Edited by sadboys
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

Well, there ya go.

 

I don't get it can you explain to me? :/

 

Nevermind I get it now, the time_end supposed to be "On Update"

Thank you for giving me an idea on how to fix it, I guess I still need to improve my sql skills thank you!

Edited by sadboys
Link to comment
Share on other sites

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.