public function afterSave($event) { $allowedFields = $this->allowed; $ignoredFields = $this->ignored; $ignoredClasses = $this->ignored_class; $newattributes = $this->getOwner()->getAttributes(); $oldattributes = $this->_oldAttributes; // Lets check if the whole class should be ignored if (sizeof($ignoredClasses) > 0) { if (array_search(get_class($this->getOwner()), $ignoredClasses) !== false) { return; } } // Lets unset fields which are not allowed if (sizeof($allowedFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($oldattributes[$f]); } } } // Lets unset fields which are ignored if (sizeof($ignoredFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($oldattributes[$f]); } } } // If no difference then WHY? // There is some kind of problem here that means "0" and 1 do not diff for array_diff so beware: stackoverflow.com/questions/12004231/php-array-diff-weirdness :S if (count(array_diff_assoc($newattributes, $oldattributes)) <= 0) { return parent::afterSave($event); } // If this is a new record lets add a CREATE notification if ($this->getOwner()->getIsNewRecord()) { $this->leaveTrail('CREATE'); } // Now lets actually write the attributes $this->auditAttributes($newattributes, $oldattributes); // Reset old attributes to handle the case with the same model instance updated multiple times $this->_oldAttributes = $this->getOwner()->getAttributes(); // get additional data for save logs $model = get_class($this->getOwner()); // Gets a plain text version of the model name $model_id = $this->getNormalizedPk(); $user_id = $this->getUserId(); // Lets get the $stamp = $this->storeTimestamp ? time() : date($this->dateFormat); // If we are storing a timestamp lets get one else lets get the date //save logs as bulk $log = new AuditTrail(); $log->saveBulk($this->log_records, $model, $user_id, $model_id, $stamp); $this->log_records = []; return parent::afterSave($event); }