CodeIgniter Forums
Log queries for auditing purposes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Log queries for auditing purposes (/showthread.php?tid=22890)



Log queries for auditing purposes - El Forum - 09-23-2009

[eluser]Unknown[/eluser]
I am building an application where there is the need to keep track of all changes made to the data stored in the db, for auditing purposes. Is there any way to retrieve a query built by Active Records, so that it can be logged somewhere? I can't just rely on the MySQL server log files, as I also need to log other information that wouldn't be available there.


Log queries for auditing purposes - El Forum - 09-23-2009

[eluser]designfellow[/eluser]
Hi,

You can use
$this->db->last_query();
to findout the last run query.

Happy Coding,
DesignFellow


Log queries for auditing purposes - El Forum - 09-23-2009

[eluser]omar-303[/eluser]
I think if you enabled the Profiler you will see all queries been made.

User Guide :
http://ellislab.com/codeigniter/user-guide/general/profiling.html


Log queries for auditing purposes - El Forum - 09-23-2009

[eluser]Unknown[/eluser]
Thanks, both of you. In the end, thanks to the fact that I am using PHP5, the solution was much easier than I thought.

I noticed that $this->db->queries would show me all the queries executed by a controller. Since I had already created a MY_Controller class, which all other controllers were extending, I simply added a __destruct() method where I look for all insert/update/delete queries and log them to the db, along with other information I need.

There might be better and more elegant ways to accomplish the task, but this will do just fine for me.