PHPackages                             sammaye/auditrail2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. sammaye/auditrail2

ActiveYii-extension[Logging &amp; Monitoring](/categories/logging)

sammaye/auditrail2
==================

A reload of the popular audit trail extension

2.1.0(10y ago)3318.1k144BSD-2-ClausePHP

Since Apr 8Pushed 10y ago9 watchersCompare

[ Source](https://github.com/Sammaye/audittrail)[ Packagist](https://packagist.org/packages/sammaye/auditrail2)[ RSS](/packages/sammaye-auditrail2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)DependenciesVersions (10)Used By (4)

audittrail
==========

[](#audittrail)

This is basically a modification of a previous extension made by [MadSkillsTisdale](http://www.yiiframework.com/user/597/) at .

I have basically cleaned up some of the code and made a few additions to the behaviour bundled within this extension.

Installing the extension
------------------------

[](#installing-the-extension)

The method of installation has changed. I have removed the need to install a module since:

- It only provided global configuration variables for the audit log widget
- It was extra bloat that didn't justify the needs
- I found that in a real system you wouldn't want a page showing all audit log entries since the audit logs
- The audit log is quite easy to add to a page using `CGridView`

As such, for these reasons, the module itself has been deleted.

### Composer

[](#composer)

This extension is listed on [packagist](https://packagist.org/packages/sammaye/auditrail2).

### Step 1

[](#step-1)

To install you must first choose a folder in which to place this repository. I have chosen:

```
/root/backend/extensions/modules

```

Since this seems most right to me. Clone this repository to that location.

### Step 2

[](#step-2)

Time to install the table. You can use the migration file provided by the original author of this extension or you can use the SQL file bundled within the migrations folder. Simply run it on your DB server (using PHPMyAdmin or something) and watch the magic unfold.

### Step 3

[](#step-3)

Reference the `AuditTrail` model within your configuration:

```
'import'=>array(
	'site.backend.extensions.modules.auditTrail.models.AuditTrail',
),

```

**Note** You can move `AuditTrail` to your `models` folder preventing you from having to link it like this.

### Step 4

[](#step-4)

Simply use the behaviour within a model like:

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
)

```

### Epilogue

[](#epilogue)

If your user class is not `User` then you may (depending on your setup) need to change the relation within the `AuditLog` model to suite your needs.

API
---

[](#api)

Please note that the below snippets are snippets only and are not tested in a real environment. It is recommend that you use this section as reference and documentation only, do not copy and paste from here.

### Custom User Attributes

[](#custom-user-attributes)

Some people don't actually have defined users but do have an attribute of the auditable model that would define a unique identification of who edited it. For this end you can use:

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'userAttribute' => 'name'
)

```

### Storing Timestamps

[](#storing-timestamps)

The date of the audit log can be changed to used timestamps instead using:

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'storeTimestamp' => true
)

```

### Changing the date format

[](#changing-the-date-format)

You can adjust the date format using the `dateFormat` property of the behaviour:

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'dateFormat' => 'Y-m-d H:i:s'
)

```

### Ignoring and allowing specific fields

[](#ignoring-and-allowing-specific-fields)

There is one interesting addition to this version. You can now specify an `allowed` set of fields and a `ignored` set of fields...or both.

To do this include the behaviour in your models like you normally would:

```
'LoggableBehavior'=> 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior'

```

But then add either an `ignored` or `allowed` (or both) list of fields to the behaviour like so:

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'allowed' => array(
		'version',
		'ns_purchase_description'
	),
	'ignored' => array(
		'ns_purchase_description',
		'ns_display_name',
		'update_time'
	)
)

```

The names put into the `allowed` and `ignored` parameters of the behaviour represent field names.

As you will notice I allow the `ns_purchase_description` field but also ignore it. When you use the fields in this way `ignored` will replace the `allowed` and this field will be omitted.

Ignoring a whole class
----------------------

[](#ignoring-a-whole-class)

This is useful if you put the behaviour in a class that extends CActiveRecord which all your own models extend from. This is useful in cases where you want ALL your classes to share the same core (like this behaviour) without having to specify it in every model you create.

```
'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
  	'ignored_class' => array(
		'ErrorLog',  // I use this to log error messages to MYSQL, no need to keep a log of this
	),
)

```

Printing out the audit log
--------------------------

[](#printing-out-the-audit-log)

Since this no longer uses a module to do its work there is no global configuration for the previously inbuilt audit log to work from. Instead you can insert an audit log like (as an example only, showing an audit of changes to a book title and it's products on a book title page):

```
$model_ids = array(array($model->id, 'Title'));
foreach($model->products as $id => $product){
    $model_ids[] = array($product->id, 'Product');
}

$criteria=new CDbCriteria(array(
    'order'=>'stamp DESC',
    'with'=>array('user'),
));
$param_id = 0;
foreach( $model_ids as $id_pair ) {
    $criteria->addCondition( '( model_id = :id' . $param_id . ' AND model = :model' . $param_id . ' )', 'OR' );
    $criteria->params[ ':id' . $param_id ] = $id_pair[0];
    $criteria->params[ ':model' . $param_id ] = $id_pair[1];
    $param_id++;
}

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'title-grid',
    'dataProvider'=>new CActiveDataProvider('AuditTrail', array(
        'criteria'=>$criteria,
        'pagination'=>array(
            'pageSize'=>100,
        )
    )),
    'columns'=>array(
        array(
            'name' => 'Author',
            'value' => '$data->user ? $data->user->email : ""'
        ),
        'model',
        'model_id',
        'action',
        array(
            'name' => 'field',
            'value' => '$data->getParent()->getAttributeLabel($data->field)'
        ),
        'old_value',
        'new_value',
        array(
            'name' => 'Date Changed',
            'value' => 'date("d-m-Y H:i:s", strtotime($data->stamp))'
        )
    ),
));

```

For more user-friendliness in the CGridView, you can use these columns:

1. Retrieves the changed model and shows its string representation:

    ```
     array(
         'header' => Yii::t('app', 'Name'),
         'value' => '$data->findModel()',
     ),

    ```
2. If the field is a relation, find the related model and use that instead of showing the FK value:

    ```
     array(
         'name' => 'old_value',
         'value' => '$data->getOldValue()',
     ),
     array(
         'name' => 'new_value',
         'value' => '$data->getNewValue()',
     ),

    ```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community26

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 67.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~113 days

Recently: every ~186 days

Total

9

Last Release

3879d ago

Major Versions

1.3.0 → 2.0.02014-08-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/7357836d822160350576f98b85244f4d5f960be303a2fcaa15a330187966cbd4?d=identicon)[Sammaye](/maintainers/Sammaye)

---

Top Contributors

[![Sammaye](https://avatars.githubusercontent.com/u/323996?v=4)](https://github.com/Sammaye "Sammaye (27 commits)")[![ferow2k](https://avatars.githubusercontent.com/u/1179833?v=4)](https://github.com/ferow2k "ferow2k (5 commits)")[![adeg](https://avatars.githubusercontent.com/u/3998049?v=4)](https://github.com/adeg "adeg (3 commits)")[![uldisn](https://avatars.githubusercontent.com/u/3525344?v=4)](https://github.com/uldisn "uldisn (3 commits)")[![kshamoun](https://avatars.githubusercontent.com/u/4984466?v=4)](https://github.com/kshamoun "kshamoun (1 commits)")[![marsuboss](https://avatars.githubusercontent.com/u/5856?v=4)](https://github.com/marsuboss "marsuboss (1 commits)")

---

Tags

Audityiitrailphundament

### Embed Badge

![Health badge](/badges/sammaye-auditrail2/health.svg)

```
[![Health](https://phpackages.com/badges/sammaye-auditrail2/health.svg)](https://phpackages.com/packages/sammaye-auditrail2)
```

###  Alternatives

[bedezign/yii2-audit

Yii2 Audit records and displays web/cli requests, database changes, php/js errors and associated data.

201657.4k4](/packages/bedezign-yii2-audit)[sammaye/yii2-audittrail

A port of audit trail

39408.2k](/packages/sammaye-yii2-audittrail)[samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

841.8M10](/packages/samdark-yii2-psr-log-target)[cornernote/yii-audit-module

Track and store usage information including page requests, database field changes and system errors.

226.6k](/packages/cornernote-yii-audit-module)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
