Skip to content

Audit Entities

Tom Anderson edited this page Mar 18, 2016 · 6 revisions

Audit Object Manager (AOM)

There are two namespaces for drivers for the AOM.
ZF\Doctrine\Audit\Entity is used for the entities in the ERD.

ZF\Doctrine\Audit\RevisionEntity is used for the entities generated by the AuditAutoloader and mapped by the AuditDriver. Entities in this namespace are referred to as RevisionEntity.

Generated Entities

Entities in the ZF\Doctrine\Audit\RevisionEntity namespace are generated from the list of entities in the configuration. The entity name is created by replacing the namespace delimiter with an underscore.

So, and audited entity with class name Database\Entity\User becomes RevisionEntity ZF\Doctrine\Audit\RevisionEntity\Database_Entity_User.

See the AuditEntityRepository for the code. Use this repository method if you need to find the audit entity class name in code from an entity class.

What to do with a RevisionEntity

Because the AOM is an object manager there's no limit to what you can do with the audit information. Much of this is unexplored at the time of this writing. But let's say you want to get the latest revision of an entity and you know it's identifier column and value, being Database\Entity\User, id, and 1.

$entityClassName = get_class($userEntity);
$auditEntityClassName = $auditObjectManager->getRepository('ZF\Doctrine\Audit\Entity\AuditEntity')->generateClassName($entityClassName);

$queryBuilder = $auditObjectManager->createQueryBuilder();
$queryBuilder->select('user')
    ->from($auditEntityClassName, 'user')
    ->innerJoin('user.revisionEntity', 'revisionEntity')
    ->andWhere('id = :id')
    ->setParameter('id', 1)
    ->orderBy('revisionEntity.id', 'DESC')
    ->setMaxResults(1)
    ;

$newestAuditEntity = $queryBuilder->getQuery()->getOneOrNullResult();
Clone this wiki locally