1. Go to this page and download the library: Download sammaye/yii2-audittrail library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sammaye / yii2-audittrail example snippets
use yii\db\ActiveRecord;
class Model extends ActiveRecord
{
public function behaviors()
{
return [
'sammaye\audittrail\LoggableBehavior'
];
}
}
class Model extends ActiveRecord
{
public function behaviors()
{
return [
'LoggableBehavior' => [
'class' => 'sammaye\audittrail\LoggableBehavior',
'ignored' => ['some_field'], // This ignores fields from a selection of all fields, not needed with allowed
'allowed' => ['another_field'] // optional, not needed if you use ignore
]
];
}
}
use yii\data\ActiveDataProvider;
use sammaye\audittrail\AuditTrail;
use common\models\Title;
use common\models\Product;
$model_ids = array(array($model->id, Title::className()));
foreach($model->products as $id => $product){
$model_ids[] = array($product->id, Product::className());
}
$criteria = AuditTrail::find();
$param_id = 0;
// $model_ids is the one you built in your original code
foreach( $model_ids as $id_pair ) {
$criteria->orWhere('model_id = :id' . $param_id . ' AND model = :model' . $param_id);
$criteria->addParams([
':id' . $param_id => $id_pair[0],
':model' . $param_id => $id_pair[1]
]);
$param_id++;
}
$criteria->orderBy(['stamp' => SORT_DESC]);
echo yii\grid\GridView::widget([
'dataProvider' => new ActiveDataProvider([
'query' => $criteria,
'pagination' => [
'pageSize' => 100,
]
]),
'columns' => [
[
'label' => 'Author',
'value' => function($model, $index, $widget){
return $model->user ? $model->user->email : "";
}
],
[
'attribute' => 'model',
'value' => function($model, $index, $widget){
$p = explode('\\', $model->model);
return end($p);
}
],
'model_id',
'action',
[
'label' => 'field',
'value' => function($model, $index, $widget){
return $model->getParent()->getAttributeLabel($model->field);
}
],
'old_value',
'new_value',
[
'label' => 'Date Changed',
'value' => function($model, $index, $widget){
return date("d-m-Y H:i:s", strtotime($model->stamp));
}
]
]
]);