PHPackages                             rmzamora/entity-audit-bundle - 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. [Database &amp; ORM](/categories/database)
4. /
5. rmzamora/entity-audit-bundle

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

rmzamora/entity-audit-bundle
============================

Audit for Doctrine Entities as forked of simplethings

v0.2(13y ago)01.2k1New BSDPHP

Since Mar 6Pushed 11y agoCompare

[ Source](https://github.com/rmzamora/EntityAudit)[ Packagist](https://packagist.org/packages/rmzamora/entity-audit-bundle)[ RSS](/packages/rmzamora-entity-audit-bundle/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (3)Used By (0)

EntityAudit Extension for Doctrine2
===================================

[](#entityaudit-extension-for-doctrine2)

[![Build Status](https://camo.githubusercontent.com/deb1c1cf8ea14cb4fcb19a3e193ede0f5f87c4cfffa1115cea091e47585aeb1b/68747470733a2f2f7472617669732d63692e6f72672f73696d706c657468696e67732f456e7469747941756469742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/simplethings/EntityAudit)

This extension for Doctrine 2 is inspired by [Hibernate Envers](http://www.jboss.org/envers) and allows full versioning of entities and their associations.

How does it work?
-----------------

[](#how-does-it-work)

There are a bunch of different approaches to auditing or versioning of database tables. This extension creates a mirroring table for each audited entitys table that is suffixed with "\_audit". Besides all the columns of the audited entity there are two additional fields:

- rev - Contains the global revision number generated from a "revisions" table.
- revtype - Contains one of 'INS', 'UPD' or 'DEL' as an information to which type of database operation caused this revision log entry.

The global revision table contains an id, timestamp, username and change comment field.

With this approach it is possible to version an application with its changes to associations at the particular points in time.

This extension hooks into the SchemaTool generation process so that it will automatically create the necessary DDL statements for your audited entities.

Installation (In Symfony2 Application)
--------------------------------------

[](#installation-in-symfony2-application)

\###Installing the bundle

Simply run assuming you have installed composer.phar or composer binary:

```
$ php composer.phar require simplethings/entity-audit-bundle
```

\###Enable the bundle

Finally, enable the bundle in the kernel:

```
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        //...
        new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),
        //...
    );
    return $bundles;
}
```

\###Configuration

Load extension "simple\_things\_entity\_audit" and specify the audited entities (yes, that ugly for now!)

\#####app/config/config.yml

```
    simple_things_entity_audit:
        audited_entities:
            - MyBundle\Entity\MyEntity
            - MyBundle\Entity\MyEntity2
```

If you need to exclude some entity properties from triggering a revision use:

\#####app/config/config.yml

```
    simple_things_entity_audit:
        global_ignore_columns:
            - created_at
            - updated_at
```

\###Creating new tables

Call the command below to see the new tables in the update schema queue.

```
./app/console doctrine:schema:update --dump-sql
```

**Notice**: EntityAudit currently **only** works with a DBAL Connection and EntityManager named **"default"**.

Installation (Standalone)
-------------------------

[](#installation-standalone)

For standalone usage you have to pass the entity class names to be audited to the MetadataFactory instance and configure the two event listeners.

```
use Doctrine\ORM\EntityManager;
use Doctrine\Common\EventManager;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;

$auditconfig = new AuditConfiguration();
$auditconfig->setAuditedEntityClasses(array(
    'SimpleThings\EntityAudit\Tests\ArticleAudit',
    'SimpleThings\EntityAudit\Tests\UserAudit'
));

$auditconfig->setGlobalIgnoreColumns(array(
    'created_at',
    'updated_at'
));

$evm = new EventManager();
$auditManager = new AuditManager($auditconfig);
$auditManager->registerEvents($evm);

$config = new \Doctrine\ORM\Configuration();
// $config ...
$conn = array();
$em = EntityManager::create($conn, $config, $evm);
```

Usage
-----

[](#usage)

Querying the auditing information is done using a `SimpleThings\EntityAudit\AuditReader` instance.

In Symfony2 the AuditReader is registered as the service "simplethings\_entityaudit.reader":

```
class DefaultController extends Controller
{
    public function indexAction()
    {
        $auditReader = $this->container->get("simplethings_entityaudit.reader");
    }
}
```

In a standalone application you can create the audit reader from the audit manager:

```
    $auditReader = $auditManager->createAuditReader($entityManager);
```

### Find entity state at a particular revision

[](#find-entity-state-at-a-particular-revision)

This command also returns the state of the entity at the given revision, even if the last change to that entity was made in a revision before the given one:

```
    $articleAudit = $auditReader->find(
        'SimpleThings\EntityAudit\Tests\ArticleAudit',
        $id = 1,
        $rev = 10
    );
```

Instances created through `AuditReader#find()` are *NOT* injected into the EntityManagers UnitOfWork, they need to be merged into the EntityManager if it should be reattached to the persistence context in that old version.

### Find Revision History of an audited entity

[](#find-revision-history-of-an-audited-entity)

```
    $revisions = $auditReader->findRevisions(
        'SimpleThings\EntityAudit\Tests\ArticleAudit',
        $id = 1
    );
```

A revision has the following API:

```
class Revision
{
    public function getRev();
    public function getTimestamp();
    public function getUsername();
}
```

### Find Current Revision of an audited Entity

[](#find-current-revision-of-an-audited-entity)

```
