If you want database changes to be logged, you have to add the AuditTrailBehavior
to the models you want to log.
<?php
/**
* Post
* @mixin AuditTrailBehavior
*/
class Post extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'bedezign\yii2\audit\AuditTrailBehavior'
];
}
}
<?php
/**
* Post
* @mixin AuditTrailBehavior
*/
class Post extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'AuditTrailBehavior' => [
'class' => 'bedezign\yii2\audit\AuditTrailBehavior',
// Array with fields to save. You don't need to configure both `allowed` and `ignored`
'allowed' => ['some_field'],
// Array with fields to ignore. You don't need to configure both `allowed` and `ignored`
'ignored' => ['another_field'],
// Array with classes to ignore
'ignoredClasses' => ['common\models\Model'],
// Is the behavior is active or not
'active' => true,
// Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp
'dateFormat' => 'Y-m-d H:i:s',
]
];
}
}
<?php
use bedezign\yii2\audit\components\Version;
$post_id = 1;
Post::findOne($post_id)->delete();
// ... time passes ...
$post = Version::find(Post::className(), $post_id);
$post->save();
<?php
use bedezign\yii2\audit\components\Version;
$post = Post::findOne(1);
$post->title = 'updated post title';
$post->save();
// ... time passes ...
$post = Version::find($post->className(), $post->id);
$post->save();
<?php
use bedezign\yii2\audit\components\Version;
// load a post
$post = Post::findOne(1);
// load the last revision
$oldPost = Version::find($post->className(), $post->id);
// save the model
$post->title = $oldPost->title;
$post->save();
<?php
use bedezign\yii2\audit\components\Version;
// get all versions
$versions = Version::versions($post->className(), $post->id));
// get the last version
$version = Version::lastVersion($post->className(), $post->id));
// load model at any version
$post = Version::find($post->className(), $post->id, $version);
// save the model
$post->save();