Insert record into audit table on change

Insert record into audit table on change

kdavid@kremzeeks.comkdavid@kremzeeks.com Posts: 4Questions: 3Answers: 0

I'm using Editor 1.5.0 with inline editing and everything works great. Is it possible to insert a record into an audit table anytime a change is made (Create/Update/Delete)? Thanks!

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    Look at the https://editor.datatables.net/reference/event/submitSuccess submitSuccess event. that's probably a good event to hook into for logging changes.

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    The best option is probably to have it done at the server-side with a server-side event. Have a read over this blog post which discusses server-side events and specifically considers the logging / audit use case.

    Allan

  • kdavid@kremzeeks.comkdavid@kremzeeks.com Posts: 4Questions: 3Answers: 0

    Thanks for the quick reply ThomD. It started to lead me in the right direction. The link you posted Allan is exactly what I was looking for! Thanks again!

  • phlpphlp Posts: 3Questions: 1Answers: 0

    Hi Allan, I am trying to do the same and I have checked out your blog post on server side events.

    Say I want to populate an audit log table each time an update is made.

    using ->on('postEdit', function ($e,$id,$values,$row){});

    So each time an edit is made, it triggers this method.

    $id returns the index
    $values returns an array of all values in the row that are being edited. (what the row is being updated to)
    $row returns the same as $values, as well as the datatable row number

    How do I retrieve what data has changed I.e. the before and after? Also what field is being updated?

    Just to check-- when the serverside script does an update, does it update the entire row vs the particular field that is being edited?

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    How do I retrieve what data has changed I.e. the before and after?

    Two options:

    1. Use the form-options's submit option set to 'changed' which will cause only the changed values to be submitted (therefore, if you see a value, you know it has changed)
    2. Add an preEdit event which will read the database for the current values and then compare them to the submitted values, one by one.

    when the serverside script does an update, does it update the entire row vs the particular field that is being edited?

    It depends. It will write to the database whatever values are submitted - which can be controlled by the client-side.

    Regards,
    Allan

This discussion has been closed.