isegrim
Založený: 11.04.2003
Príspevky: 2
Zaslal: 2003-04-11 09:20
Návrat hore
Odpovedať s citátom
|
following problem:
I have a table that includes a 'date' field. Now I want to automatically update the date to the actual time/date whenever a change is made to that entry.
I guess this can be realized using a "update_after"-trigger ... ?!
Could anyone give me a hint of how that .inc file could/should look like ?
|
nepto
Založený: 06.01.2003
Príspevky: 320
Bydlisko: Phoenix, Arizona (USA)
Zaslal: 2003-04-11 16:22
Návrat hore
Odpovedať s citátom
|
The
$opts['triggers']
option has to be outside column definitions. It has nothing to do with column definitions anyway, however it can modify them (but in triggers you can perform several other actions).
Concerning to the initial post, there are basically two approaches of doing automatic timestamp update. I will describe both. It may be long.
FIRST APPROACH
This is based on the fact, we want to have actual data (in our case date) in database every time a record change is submited (a record is updated). You can mark field "hidden" (see 'H' flag) with other appropriate flags (for example 'ACPH'). Hidden is hidden, however its value is inserted into database. So before starting phpMyEdit class we will overwrite is value with desirable one. You can use following command for this purpose:
| Kód: |
| $opts['cgi']['overwrite']['col_name'] = 'value'; |
phpMyEdit will look into CGI environment for particular field value. But the command aobve overwrites this value and set the new one.
SECOND APPROACH
Second approach is based on triggers. Firstawal, column has to be readonly or disabled. It can be even editable, but note that value will be desirably overwritten after every update.
There are basically two way how to do this using triggers. First one is performing an additional query after record is updated to set right value. Second one is to set variables available in before triggers, so the update operation can update also desirable column in addition to normal submitted fields. I have never tested this case, but it may work somehow.
Back to the first case. Looking at the documentation I see, there are several usable variables available. Important and essential for us is
$this->rec
(primary key value). There are also some class/object method calls avaiable. The essential for us is
$this->myQuery()
call. Something like this have to work in after update trigger file:
| Kód: |
<?php
return
$this->myQuery(sprintf('UPDATE table SET date_column = NOW() WHERE id = %d',
$this->rec));
?> |
OK, I hope this will help you. Feel free to ask more if there are still some unclear things.
|
nepto
Založený: 06.01.2003
Príspevky: 320
Bydlisko: Phoenix, Arizona (USA)
Zaslal: 2003-04-11 17:46
Návrat hore
Odpovedať s citátom
|
For this particular purpose can be very well used:
- marking field as read-only with
['options']
- setting default value via
['default']
|
nepto
Založený: 06.01.2003
Príspevky: 320
Bydlisko: Phoenix, Arizona (USA)
Zaslal: 2003-04-11 18:45
Návrat hore
Odpovedať s citátom
|
| BillSaysThis Napísal: |
| Second, is it possible to specify a function call, like Now(), for the default value? Otherwise, how would I ensure that the correct date gets used? |
| Kód: |
| $opts['fdd']['datetime_column']['default'] = date('Y-m-d H:i:s'); |
|
MSidenius
Založený: 21.09.2004
Príspevky: 3
Zaslal: 2004-09-21 14:30
Návrat hore
Odpovedať s citátom
|
| isegrim Napísal: |
following problem:
I have a table that includes a 'date' field. Now I want to automatically update the date to the actual time/date whenever a change is made to that entry.
I guess this can be realized using a "update_after"-trigger ... ?!
Could anyone give me a hint of how that .inc file could/should look like ? |
What about this easy one:
Add a field of type "TIMESTAMP" with default value "NULL" to your table in MySql. If you dont give a value for this field whenever the record is saved/changed MySQL will automatically set the current time to the field. To avoid phpMyEdit to give the field a value just set: 'required' => false, for the field. It works great. Be aware that TIMESTAMP fields has changed behaviour in 4.1 and up, but it still works this way (http://dev.mysql.com/doc/mysql/en/TIMESTAMP_4.1.html)
Regards,
Michael
|
ajh
Založený: 17.12.2003
Príspevky: 236
Zaslal: 2004-09-22 21:47
Návrat hore
Odpovedať s citátom
|
The simplest way of doing this is to create a date/time column which has a 'sqlw' option which sets the value to NOW. You can also set the field to be read only. That way whenever you update the records, that field wil always be set to current date and time
|
paolodp
Založený: 06.07.2006
Príspevky: 11
Zaslal: 2006-08-04 07:25
Návrat hore
Odpovedať s citátom
|
hi,
I'd like to save datetime only when another particular field is changed, not every time the record is edited.
For example, field IdState is changed, field DateState is automatically updated .
Someone has an idea ?
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2006-08-04 12:19
Návrat hore
Odpovedať s citátom
|
in trigger insert before something like:
if($oldvals['field'] != $newvals['field']) $newvals['field2'] = date();
|
Justin Baker
Založený: 10.04.2007
Príspevky: 13
Zaslal: 2007-04-17 13:49
Návrat hore
Odpovedať s citátom
|
I basically want the same as the other user. i want the date to change automatically every time someone edits any of the field in a particular row. So when he wants to edit a title of a "condition" of a specific book, it will auto fix the date to the present date.
By reading this thread, I have gotten it to work ONLY WHEN You add a NEW title to the book list! The NEW DATE will show up on the add/edit page.
If I change the condition of specific book thats ALREADY in the database. the ADD/EDIT page shows the previous date and then it doesnt change it when i change the condition of the book?
Is this what I need to add just before it calls the CLASS.php after everything else?:
$opts['cgi']['overwrite']['date'] = date('Y-m-d');
Can you please give more details on what to add where?!? this is very confusing?!
Thank you in advance!
Justin
|
Justin Baker
Založený: 10.04.2007
Príspevky: 13
Zaslal: 2007-04-18 12:51
Návrat hore
Odpovedať s citátom
|
OK After playing around with different trggiers, I finally figured out how to fix the date to change to the current date when ever a change to the entry is made. So when ever you change a books condition, thats already in the database, it will change the date automacally to the new date. Here is what I put in the DATE column array!
'sqlw' => 'IF($val_qas != NOW(), NOW(), $val_qas)'
Just before the closing of the array so it doesnt need the end comma. I guess it doesnt make any difference where in the array it goes, just that it works now!
Man I am really starting to understand some of the aspects if this coding structure.
|
k4mg
Založený: 06.05.2007
Príspevky: 9
Zaslal: 2007-05-06 16:45
Návrat hore
Odpovedať s citátom
|
OK, I have same problem as original poster, I have a "modified" field I want to have the current date in for every add or update. I believe I have tried both the suggested solutions to this problem, however my modified field is still blank. Here is what I tried:
$opts['fdd']['modified'] = array(
'name' => 'Modified',
'default' => date('Y-m-d'),
'select' => 'T',
'options' => 'LFR', //list, filter, read only
'maxlen' => 19,
'sort' => true,
'sqlw' => 'IF($modified != NOW(), NOW(), $modified)'
);
What is it I have done wrong?
Thanks, Barry
|
k4mg
Založený: 06.05.2007
Príspevky: 9
Zaslal: 2007-05-07 10:33
Návrat hore
Odpovedať s citátom
|
I made some new effortds, one which didn't work is commented out, but I tried them all. The modified (datetime field) field is always blank after an insert or edit. I have tried 'default', 'sqlw', and 'cgi overwrite'. Others have gotten this to work, what am I doing wrong?
Last code:
$opts['fdd']['modified'] = array(
'name' => 'Modified',
'default' => date('Y-m-d H:i:s'),
'select' => 'T',
'options' => 'LFR', //list, filter, read only
'maxlen' => 19,
'sort' => true,
// 'sqlw' => '($modified != NOW()? NOW(): $modified)'
'sqlw' => date('Y-m-d H:i:s')
);
$opts['cgi']['overwrite']['modified'] = date('Y-m-d H:i:s');
Thanks, Barry
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2007-05-07 10:39
Návrat hore
Odpovedať s citátom
|
'options' => 'LFR', //list, filter, read only
means that this field is active only in list,filter (not in add/change). if you add AC there the field will be visible, so it's best to use before trigger and change $newvals;
|
k4mg
Založený: 06.05.2007
Príspevky: 9
Zaslal: 2007-05-07 11:30
Návrat hore
Odpovedať s citátom
|
| michal Napísal: |
'options' => 'LFR', //list, filter, read only
means that this field is active only in list,filter (not in add/change). if you add AC there the field will be visible, so it's best to use before trigger and change $newvals; |
Michal,
Thank you, thank you! As soon as I added the 'AC' or eliminated options entirely, I started to get all zeros, which was a great improvement over blank. I changed to NOW() and it works! I left in (but commented out) all the other ways I tried, which probably also work. Now to figure out where you said I should put it. Final code looks like this, for anyone else who needs this technique:
$opts['fdd']['modified'] = array(
'name' => 'Modified',
// 'default' => 'NOW()',
'select' => 'T',
// 'options' => 'ACLF',
'maxlen' => 19,
'sort' => true,
'sqlw' => 'NOW()'
);
//$opts['cgi']['overwrite']['modified'] = 'NOW()';
My first use of this utility was yesterday, and it is a great time saver. Thanks to all it's authors.
Barry
|
agadkarmohan
Založený: 05.09.2007
Príspevky: 1
Zaslal: 2007-09-05 06:00
Návrat hore
Odpovedať s citátom
|
Hi,
You can just change your date field to the following
ALTER TABLE table_name CHANGE field_name field_name TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
The above will result in following
1. Inserts current date while creating the record and
2. Updates to current date if at all any changes are
made to the row.
Hope this is useful.
| isegrim Napísal: |
following problem:
I have a table that includes a 'date' field. Now I want to automatically update the date to the actual time/date whenever a change is made to that entry.
I guess this can be realized using a "update_after"-trigger ... ?!
Could anyone give me a hint of how that .inc file could/should look like ? |
|
|
Odoslať novú tému
Odpovedať na tému
|