I have implemented the below timestamp behaviour. Its "created_at" time is working fine. But when i am updating the page the "updated_at" is showing the same time as of "created_at" time.
[b]
Can you help me how to get the "updated_at" time/where am I making mistake??[/b]
One more thing can I get the time difference between "created_at" and "updated_at" time??
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => new Expression('NOW()'),
],
];
}
I have an application page which is throwing random integer. Only on submit the data is getting saved. That’s why the “created_at” and “updated_at” time are same.
[b]Is there any way, for the situation where I can store the refreshed time of the page. For first time when the page refreshed the page it will hold the time which will saved as "created_at" on submit and on submit it will update the "updated_at" time?
[/b]
Please find the attached diagram for the application timeline structure.
Your timestamp behavior should work as expected. As MetaCrawler has pointed, it will set the current time to ‘created_at’ and ‘updated_at’ when a record is newly created and will set the current time only to ‘updated_at’ when a record is updated.
The problem seems to lie in the way you are creating and/or updating the records. I guess you might be creating new records all the time, not updating the existing records.
That’s correct. Every time creating new record. Its not updating the same record later stage.
In my case, when page is getting refreshed and creating some random number, which i am holding in a hidden input form without saving into DB. Once the submit button is pressed, the value which was on hold in the hidden input is getting saved and instantly page get refreshed and throw new set of number.
But the time when the page is created and submission of button, there is time gap, may it be in seconds. But I want to capture that time difference between the page creation time and the model’s save time.
Thanks for the input. I have already created a field in DB.
Currently i am holding the time of the page creation in a text input field, which is getting saved on form submission. Its working.
But I am actually finding some solution, if it can be done through timestamp behviour. If yes, how to do that. Because created_at and updated_at only save time when the model is saved. But the page creation time is few seconds/minutes before the model save.