/** * Creates and saves a new log entry. Only properties that have been changed will be saved. * * @param int $model_id * the id of this model * @param $model_type * the class name of the model * @param $action * the controller action * @param null $old_attr * array of properties * @param null $new_attr * array of properties * @return bool * whether the log entry has been saved */ protected function addLogEntry($model_id, $model_type, $action, $old_attr = null, $new_attr = null, $eventData) { if ($action == self::ACTION_VIEW || $action == self::ACTION_CREATE) { $entry = LogEntry::findOne(['model_id' => $model_id, 'model_type' => $model_type, 'action' => $action, 'created_by' => \Yii::$app->user->id]); if (!is_null($entry)) { return false; } } $logEntry = new LogEntry(); $logEntry->model_id = $model_id; $logEntry->model_type = $model_type; $logEntry->action = $action; if (!is_null($old_attr) && !is_null($new_attr)) { if ($eventData != null && $eventData->hasProperty('attributes')) { /** * @var LogEvent $eventData */ foreach ($eventData->attributes as $key => $value) { $new_attr[$key] = $value; $old_attr[$key] = ''; } } if (count(array_diff_assoc($old_attr, $new_attr))) { $logEntry->old_attr = array_diff_assoc($old_attr, $new_attr); $logEntry->new_attr = array_diff_assoc($new_attr, $old_attr); $logEntry->old_attr = json_encode($logEntry->old_attr); $logEntry->new_attr = json_encode($logEntry->new_attr); } else { if ($action == self::ACTION_UPDATE) { return false; } } } return $logEntry->save(true); }
/** * See @jonas91\loggablebehavior\models\LogEntry * * @return mixed */ protected function loadLogs($pageSize = 5) { return LogEntry::getLogs($this->model, $pageSize); }
/** * @param ActiveRecord $model * @return bool */ public static function hasEntry(ActiveRecord $model) { if (!is_null($model)) { $logEntry = LogEntry::findOne(['model_id' => $model->id]); if (!is_null($logEntry)) { return true; } } return false; }