2012-02-24 37 views
1

我有要求在zend項目中實現審計記錄功能。模型是使用zend db創建的,更新函數如下。zend db的審計記錄功能

public function updateGroup($data,$id) 
{  
    $row = $this->find($id)->current(); 
    // set the row data 
    $row->name     = $data['name']; 
    $row->description   = $data['description']; 

    $row->updatedBy   = $data['updatedBy']; 
    $row->updatedOn   = date('Y-m-d H:i:s'); 

    $id = $row->save(); 
    return $id; 
} 

我必須創建一個包含當前用戶ID的auditlog信息表。我嘗試了很多方法,沒有什麼是一個好的解決方案。 zend的良好審計日誌記錄功能的最佳做法是什麼?

我只想記錄修改後的數據。和日誌表架構就像是

id, 
table, 
column, 
rowId 
oldvalue, 
newvalue, 
updatedon, 
updatedbyuser 

回答

3

使用Zend_Log_Writer_Db

Zend_Log_Writer_Db將日誌信息寫入使用 Zend_Db的數據庫表。 Zend_Log_Writer_Db的構造接收 Zend_Db_Adapter會例如,表的名稱,以及數據庫 列的事件數據項

例如映射:

$columnMapping = array('name' => 'name', 
         'desc' => 'desc', 
         'updatedBy' => 'userid', 
         'updatedOn' => 'date'); 
$writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping); 

$logger = new Zend_Log($writer); 


$logger->setEventItem('name', $data['name']); 
$logger->setEventItem('desc', $data['name']); 
$logger->setEventItem('updatedBy',$data['updatedBy']); 
$logger->setEventItem('updatedOn',date('Y-m-d H:i:s')); 

EDIT:只記錄修改後的數據:

public function logUpdate(array $values) 
{ 
    $columnMapping = array('id' => 'id', 
          'table' => 'table', 
          'column' => 'column', 
          'rowId' => 'rowId', 
          'oldvalue' => 'oldvalue', 
          'newvalue' => 'newvalue', 
          'updatedon' => 'updatedon', 
          'updatedbyuser' => 'updatedbyuser'); 

    $writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping); 

    $logger = new Zend_Log($writer); 


    $logger->setEventItem('id', $values['id']); 
    $logger->setEventItem('table', $values['table']); 
    $logger->setEventItem('column', $values['column']); 
    $logger->setEventItem('rowId', $values['rowId']); 
    $logger->setEventItem('oldvalue', $values['oldValue']); 
    $logger->setEventItem('newValue', $values['newValue']); 
    $logger->setEventItem('updatedon', $values['updatedon']); 
    $logger->setEventItem('updatedbyuser', $values['updatedbyuser']); 
} 

和updateGroup:

public function updateGroup($data,$id) 
{  
    $row = $this->find($id)->current(); 

    $values = array('table' => $this->name); 
    $values = array('updatedon' => $data['updatedBy']); 
    $values = array('updatedbyuser' => date('Y-m-d H:i:s')); 
    //go through all data to log the modified columns 
    foreach($data as $key => $value){ 
     //check if modified log the modification 
     if($row->$key != $value){ 
     $values = array('column' => $key); 
     $values = array('oldValue' => $row->$key); 
     $values = array('newValue' => $value); 
     logUpdate($values); 
     } 
    } 

    // set the row data 
    $row->name     = $data['name']; 
    $row->description   = $data['description']; 

    $row->updatedBy   = $data['updatedBy']; 
    $row->updatedOn   = date('Y-m-d H:i:s'); 

    $id = $row->save(); 


    return $id; 
} 

請注意,它更好地實現所有應用程序的日誌記錄和單獨更新日誌記錄,請參閱this answer

+0

感謝您的回答。但是,這將再次將所有信息寫入數據庫?我只想記錄修改後的數據。和日誌表模式是像id,表,列,oldvalue,newvalue,updatedon,updatedbyuser – codlib 2012-02-24 07:36:50

+0

我添加了一個編輯,希望它有幫助 – 2012-02-24 08:56:30

+0

這解決了我的問題。非常感謝。 – codlib 2012-02-24 09:10:18