Coder Social home page Coder Social logo

entity-audit's Introduction

EntityAudit Extension for Doctrine2

Build Status

This extension for Doctrine 2 is inspired by Hibernate Envers and allows full versioning of entities and their associations.

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

###Installing the lib/bundle

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

$ composer require simplethings/entity-audit

For standalone usage you have to pass the EntityManager.

use Doctrine\ORM\EntityManager;
use SimpleThings\EntityAudit\AuditManager;

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

$auditManager = AuditManager::create($em);

Usage

Define auditable entities

You need add Auditable annotation for the entities which you want to auditable.

use Doctrine\ORM\Mapping as ORM;
use SimpleThings\EntityAudit\Mapping\Annotation as Audit;

/**
 * @ORM\Entity()
 * @Audit\Auditable()
 */
class Page {
 //...
}

You can also ignore fields in an specific entity.

class Page {

    /**
     * @ORM\Column(type="string")
     * @Audit\Ignore()
     */
    private $ignoreMe;

}

Use AuditReader

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

You can create the audit reader from the audit manager:

$auditReader = $auditManager->createAuditReader();

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::class,
    $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

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

A revision has the following API:

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

Find Changed Entities at a specific revision

$changedEntities = $auditReader->findEntitiesChangedAtRevision(10);

A changed entity has the API:

class ChangedEntity
{
    public function getClassName();
    public function getId();
    public function getRevisionType();
    public function getEntity();
}

Find Current Revision of an audited Entity

$revision = $auditReader->getCurrentRevision(
    'SimpleThings\EntityAudit\Tests\ArticleAudit',
    $id = 3
);

Setting the Current Username

Each revision automatically saves the username that changes it. For this to work, the username must be resolved.

You can username callable to a specific value using the AuditConfiguration.

$auditConfig = new \SimpleThings\EntityAudit\AuditConfiguration();
$auditConfig->setUsernameCallable(function () {
	$username = //your customer logic
    return username;
});

Supported DB

  • MySQL / MariaDB
  • PostgesSQL
  • SQLite

We can only really support the databases if we can test them via Travis.

Contributing

Please before commiting, run this command ./vendor/bin/php-cs-fixer fix --verbose to normalize the coding style.

If you already have the fixer locally you can run php-cs-fixer fix ..

entity-audit's People

Contributors

andrewtch avatar artursvonda avatar atsiddiqui avatar beberlei avatar bendavies avatar bobvandevijver avatar c0ntax avatar davidbadura avatar dunglas avatar ivariable avatar jamescdavis avatar javierjimenez avatar merk avatar mikemeier avatar mweimerskirch avatar oskarstark avatar pborreli avatar royopa avatar rvanlaak avatar saem avatar shira-3749 avatar smoench avatar soullivaneuh avatar theratg avatar tolry avatar tommygnr avatar tseho avatar tyx avatar userfriendly avatar welante avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

entity-audit's Issues

Using with batch inserts/updates causes memory leak

Q A
Bug report? yes
Feature request? no
BC Break report? no

Steps to reproduce:

  1. Create a console application with thousands of inserts/updates, preferable with any progress bar,
  2. Observe that IOPS amount per second is dropping as the import goes by.

I'll post a PR with fix in a couple of moments because it required some digging.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.