CodeIgniter Forums
Audit tables - get original data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Audit tables - get original data (/showthread.php?tid=76248)



Audit tables - get original data - korgoth - 04-25-2020

Hello guys, 

i am trying to figure out a way to create audit tables for some of my project tables (orders, users, etc) in which i write each time an entry gets updated. 

I have read quite a bit around and tried to make that using database triggers as a cleaner way, but that just has too many things that do not suite me. 

So i decided to use the framework and implement table audits myself using CI4 model events. using afterInsert, afterUpdate, afterDelete seems to be a greate way to do it, however the data passed to those events is only the data which changes (which is awesome to have) - but i am looking at a way to get the original row, thats being updated. This way in my audit table i can have the old (original) row, and additional columns: who, when and what was changed. 

While working with Entities makes that somewhat possible (since Entities have the "original" property) - this quickly gets out of hand if i try to make a simple query that does not make use of the Entities - say i want to mass update order status for 500 orders or something. 

I am quite stuck and just cant figure out a relatively efficient way of doing that. 

I always thought that this should be somewhat generic and expected to find tons of solutions  - however that does not seem to be the case Smile

Anyone has any ideas?