2015-10-06 86 views
0

我創建了UserActivityListener來記錄用戶操作時做類似CRUD的任何操作。但我想在獲得$實體字段數據時遇到問題:CakePHP 3:如何獲取原始實體數據和修改的實體數據?

例如:我得到了表Users(id,email,password);

// I do override afterSave event 
public function afterSave(Event $event, Entity $entity, ArrayObject $options) { 
    //Log field entity 
    if($entity->isNew()) { 
    Log::write('info', $entity->get('email')); 
    // This one is fine 
    } else { 
    Log::write('info-old-field-data', $entity->get('email')); 
    // old data of field was fine because entity does not change yet 
    Log::write('info-new-field-data', $entity->get('email')); 
    // Wrong, entity not yet change, Please help me to get new data. 
    } 
} 

如何在afterSave函數中獲取實體的新數據? 如何檢查哪些字段被修改,哪些不是?

+0

http://stackoverflow.com/q/26548511/1127933 – Salines

回答

0

謝謝大家查看我的問題。我找到了解決上述問題的方法。 我搬到我的代碼,以正確的型號事件:afterSaveCommit

//I do override afterSaveCommit function in Table class 
public function afterSaveCommit(Event $event, Entity $entity, ArrayObject $options) { 
    //Log field entity 
    if($entity->isNew()) { 
     Log::write('info', $entity->getOriginal('email')); 
    } else { 
     Log::write('info-old-field-data', $entity->getOriginal('email')); 
     Log::write('info-new-field-data', $entity->get('email')); 
    } 
} 
相關問題