D Oliveira wrote: ↑2022-02-11 10:04
The goal is to track which field the user has changed, not just the table, field name and timestamp
In the past I did this by adding a changes table holding...
- id
- tableName
- pkValue
- createdOn
- createdBy
- data (text field)
On after_insert, after_update and before_delete I save the record's data as JSON into the data field next to meta data. So this is the first step. Using createdOn or id column I am able to exactly sort changes per table/pk. I created a custom page for displaying all changes in descending order.

- CBBc5NDT91.png (117.21 KiB) Viewed 3110 times
Additionally I modified the DV of that log-entries table to show differences:

- Wlk5S1xGca.png (125.17 KiB) Viewed 3110 times
I've implemented some kind of diff-algorithm which automatically displays new entries, modified entries and deleted entries

- chrome_qK6TzMNATv.png (4.86 KiB) Viewed 3110 times
I hate repeating code, so I've wrapped everything in a PHP class per Business Object (AKA relevant table) inheriting from a baseclass. On every table-hook I only have to write this:
Code: Select all
function TABLENAME_after_insert($data, $memberInfo, &$args){
$object = new \NAMESPACENAME\TABLENAME();
return $object->afterInsert($data, $memberInfo, $args);
}
function TABLENAME_after_update($data, $memberInfo, &$args){
$object = new \NAMESPACENAME\TABLENAME();
return $object->afterUpdate($data, $memberInfo, $args);
}
function TABLENAME_before_delete($selectedID, &$skipChecks, $memberInfo, &$args){
$object = new \NAMESPACENAME\TABLENAME();
return $object->softDelete('id', $selectedID, 'status_id', 3, $memberInfo, $args);
}
Known issues
I'm not satisfied with storing the foreign keys of lookups, only. It would be better if the library could automatically resolve the foreign keys and additionally save data from the looked up record. See 3rd screenshot: Instead of showing status_id diff "4" to "2" I'd like to see for example diff "Status NEW" to "Status CONFIRMED" or whatever is the meaning of 4 and 2. Don't know how to say. Hope you got my point.
I hope this gives you an idea.