sadboys Posted September 6, 2017 Share Posted September 6, 2017 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, ]); } } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 6, 2017 Share Posted September 6, 2017 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 Quote Link to comment Share on other sites More sharing options...
sadboys Posted September 6, 2017 Author Share Posted September 6, 2017 (edited) 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 September 6, 2017 by sadboys Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 6, 2017 Share Posted September 6, 2017 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! Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 6, 2017 Share Posted September 6, 2017 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?) Quote Link to comment Share on other sites More sharing options...
sadboys Posted September 6, 2017 Author Share Posted September 6, 2017 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. Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 6, 2017 Share Posted September 6, 2017 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. Quote Link to comment Share on other sites More sharing options...
sadboys Posted September 6, 2017 Author Share Posted September 6, 2017 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: Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 6, 2017 Share Posted September 6, 2017 Well, there ya go. Quote Link to comment Share on other sites More sharing options...
sadboys Posted September 6, 2017 Author Share Posted September 6, 2017 (edited) 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 September 6, 2017 by sadboys Quote Link to comment Share on other sites More sharing options...
Solution Sepodati Posted September 6, 2017 Solution Share Posted September 6, 2017 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). Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2017 Share Posted September 6, 2017 I'd set it up with start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, end_time DATETIME DEFAULT NULLWhen you create a ticket the record will get the starttime updated automatically. To close the ticket... UPDATE mytable SET end_time = NOW(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.