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?
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…
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.