Logging each Insert/Update/Delete

Hi all,

I am using Yii activerecord mechanism in my application to manipulate the data. I have created another table to log each insert/update/delete in each table of my database. The table is something like this.

Id int,

table_name varchar,

executed_query varchar,

execution_date datetime,

ip_address varchar

Now I want to store the queries (insert/update/delete) performed via ActiveRecord on each model to be stored in above table. Can anyone help me to implement this mechanism?

Thanks in advance

I’m not sure if you can do that directly in yii without changing a bunch of underlying code.

This isn’t exactly what you’re asking but thought i’d throw it at you as an idea.

If you’re using mysql for your db, it can log all queries to a file. Perhaps you could use this file for your functionality.

Food for thought. Good luck.

Thanks CAM C, But I want it to be stored in database table. I am using mysql.

I’m not 100% sure, but it seems like all of Yii’s queries come from CDbCommand.queryInternal(). Unfortunately, I don’t know how easy/hard it would be to extend this class properly.

Worst case scenario: you edit this core file directly…

we Have done with These Things using both back end and using Yii

Suggestion go with database trigger using with mysql or oracle having idea for that contact me

Yes database trigger is an option for me but I wanted it to be done using Yii :)

You can create your own ActiveRecord class, something like this




class ActiveRecord extends CActiveRecord {

	protected function beforeDelete() {

		// do the logging

		return parent::beforeDelete();

	}

}


class MyModel extends ActiveRecord {

	...


}



Or use some kind of loggable behavior. I’m pretty sure there are a lot of them.