bedezign/yii2-audit

Diff: Old attributes are always empty

codyng opened this issue · 3 comments

Hi there,

Why the old attributes for the Diff are always empty?

Many Thanks!!

Usually that means that the previous state is unknown... Can you elaborate more and perhaps show what you've tried? It is pretty hard to diagnose issues with so little information.

Hi, firstly thx for your great work on this project.

What I tried to do is to implement the actionUpdate() method in the CMS.

Below is the simple pattern from the Gii:
public function actionUpdate($id) {
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post())) {

	if ($model->validate()) {
		// modify some field values...
		// ...
		// ...

		if ($model->save()) {
			// ...
		}
	
	}
}

}

The model data is updated by the CMS user inside the ActiveForm. The data is submitted with POST method. I just think $model->load() overrides the model data. No original data is kept before doing the Active model update, i.e. $model->load() executes before the AuditTrailBehavior events.

I tried to monitor $this->owner->getAttributes() inside AuditTrailBehavior::afterUpdate() or beforeUpdate() (what I tried to add ActiveRecord::EVENT_BEFORE_VALIDATE). The $this->owner->getAttributes() return the data from the form but not the original data model.

Or what I missed out something or did the wrong update flow?

Many thanks!!

Hi all,

The problem is solved now. $model->afterFind() must be called after finding the model.

Thx,