开发者

Implementing log of actions

Is there any class available to do auditing of actions of users? I mean actions that users do with add, delete or modify a record?

Some time ago I've used phpmyedit and It has a "logtable" option ($opts['logtable'] = 'changelog';), that one can use to "audit" or track the activity to that special table.

Is it possible 开发者_Go百科to do this or do I have to implement it (for instance) before $f->update()?


There is no such class, but I have implemented this several times. This involves "pushing" standard Model_Table class to include functionality into ALL of your models. Here is recipe.

  1. Create your audit log model class. Model_AuditLog probably would be a good name.
  2. Create file in lib/Model/Table.php which replaces common ancestor of all your models based on the code below.
  3. Write similar code for beforeUpdate / afterUpdate and beforeDelete / afterDelete

You can omit "update" from "beforeInsert" if you are only interested in successful operations. Create some tests. Don't forget to make sure Model_Audit does NOT inherit your class to avoid recursion. You can actually name class differently, as long as you remember to use it for the models.

Agile Toolkit will have seamless audit support through Controller at some point, most probably in 4.2.


class Model_Table extends Model_MVCTable {

    function beforeInsert($data){
        $this->insert_audit = $this->add('Model_Audit')
            ->set('action','insert');
        $this->insert_audit->update();
        return parent::beforeInsert($data);
    }
    function afterInsert($id){
        $this->insert_audit->set('is_completed',true)
            ->update();
        return parent::afterInsert($id);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜