I have two mysql datetime fields in a table which I am using as part of a grid:
They have the following attributes when you look at them phpMyAdmin:
NAME, TYPE, NULL, DEFAULT, EXTRA
last_updated, timestamp, No, current_timestamp(), ON UPDATE CURRENT_TIMESTAMP()
created, timestamp, No, current_timestamp()
When I edit a record in the table in phpMyAdmin, these fields work correctly. i.e. the last_updated field is updated with the current timestamp automatically.
The problem is that the last_updated field is not being updated when I make any changes in gridphp using the modal editor.
My fields are defined like this:
$col = array();
$col[“title”] = “Last Updated”;
$col[“name”] = “last_updated”;
$col[“width”] = “60”;
$col[“hidden”] = true;
$col[“editable”] = true;
$col[“formatter”] = “datetime”;
$col[“formatoptions”] = array(“srcformat”=>’Y-m-d H:i:s’,”newformat”=>’Y-m-d H:i:s’,”opts” => array());
$col[“show”] = array(“list”=>false, “add”=>false, “edit”=>true, “view”=>true);
$col[“editoptions”] = array(“readonly”=>”readonly”, “style”=>”border:0; background:#EEE”);
How can I make the last_updated field update on any edits/changes to a record in gridphp?
Thanks,
pcatt
Does your table have a trigger or a default-value for the last_updated field? I suspect it-does. In that-case, you do-not want the PHPGrid to update that field. You want the database trigger to set that field.
I have this situation quite often – so I always display the last updated field as read-only on the Grid (CREAT_DATE in my case), so edit=true.
Thanks
Mike
Hi Mike,
Thanks for your reply. I was a good idea, but it doesn’t doesn’t work when I set the field to read-only:
$col = array();
$col[“title”] = “Last Updated”; // caption of column, can use HTML tags too
$col[“name”] = “last_updated”; // grid column name, same as db field or alias from sql
$col[“width”] = “180”; // width on grid
$col[“hidden”] = false;
$col[“editable”] = true;
$col[“formatter”] = “datetime”;
$col[“formatoptions”] = array(“srcformat”=>’Y-m-d H:i:s’,”newformat”=>’Y-m-d H:i:s’,”opts” => array());
$col[“editoptions”] = array(“readonly”=>true, “style”=>”border:0; background:#EEE”);
$cols[] = $col;
in phpmyadmin, the last_updated field is defined:
type: timestamp
null: no
default: current timestamp
extra: ON UPDATE CURRENT_TIMESTAMP()
Am I doing something wrong?
Thanks
Update:
What’s interesting is that I commented out the entire block for the field definition, and then edited a record. The last_update field in the database was then updated.
So somehow, my field definition is interfering with mysql doing its: ON UPDATE CURRENT_TIMESTAMP()
Update again:
I managed to get it to update with the following definition:
$col = array();
$col[“title”] = “Last Updated”; // caption of column, can use HTML tags too
$col[“name”] = “last_updated”; // grid column name, same as db field or alias from sql
$col[“width”] = “180”; // width on grid
$col[“hidden”] = false;
$cols[] = $col;
This means the field is not show in the edit modal popup. Which is OK, I suppose, but it would be nice to get to the bottom of what the cause of the problem was.
I guess the solution was just to set:
$col["editable"] = false;
Editable fields are not posted back to server. If it’s editable, it will be posted and whatever values is there in it, it will be the part of update query.