Jump to content
sadboys

Both fields update automatically?

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

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 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

So?  Obviously you haven't done the proper debuggin to find out why you are updating two fields when you only want to update one.  I'd look at my query statement if I were you!

Share this post


Link to post
Share on other sites

Where does the field get updated? Is this all in a database and that field gets updated or is it a field on an HTML page? (or both, if one feeds the other?)

Share this post


Link to post
Share on other sites

Where does the field get updated? Is this all in a database and that field gets updated or is it a field on an HTML page? (or both, if one feeds the other?)

 

Yes this is all in a database.

Share this post


Link to post
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.

Share this post


Link to post
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:

 

 

Gr74REUESCS8Vw-RBfNG-Q.png

Share this post


Link to post
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

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites

I'd set it up with

start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
end_time DATETIME DEFAULT NULL
When you create a ticket the record will get the starttime updated automatically.

 

To close the ticket...

UPDATE mytable SET end_time = NOW();

Share this post


Link to post
Share on other sites

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.