Skip to content

Commit

Permalink
AuditReader:getEntityTableAuditName (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhelias authored and DamienHarper committed Jul 5, 2019
1 parent 42a8d00 commit f7a59e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/DoctrineAuditBundle/Reader/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Statement;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Pagerfanta\Adapter\DoctrineDbalSingleTableAdapter;
use Pagerfanta\Pagerfanta;

Expand Down Expand Up @@ -199,19 +200,11 @@ private function getAuditsQueryBuilder($entity, $id = null, ?int $page = null, ?
}

$connection = $this->entityManager->getConnection();
$schema = $this->entityManager->getClassMetadata(\is_string($entity) ? $entity : \get_class($entity))->getSchemaName();

$auditTable = implode('', [
null === $schema ? '' : $schema.'.',
$this->configuration->getTablePrefix(),
$this->getEntityTableName(\is_string($entity) ? $entity : \get_class($entity)),
$this->configuration->getTableSuffix(),
]);

$queryBuilder = $connection->createQueryBuilder();
$queryBuilder
->select('*')
->from($auditTable, 'at')
->from($this->getEntityAuditTableName($entity), 'at')
->orderBy('created_at', 'DESC')
->addOrderBy('id', 'DESC')
;
Expand Down Expand Up @@ -247,22 +240,14 @@ private function getAuditsQueryBuilder($entity, $id = null, ?int $page = null, ?
public function getAudit($entity, $id)
{
$connection = $this->entityManager->getConnection();
$schema = $this->entityManager->getClassMetadata(\is_string($entity) ? $entity : \get_class($entity))->getSchemaName();

$auditTable = implode('', [
null === $schema ? '' : $schema.'.',
$this->configuration->getTablePrefix(),
$this->getEntityTableName(\is_string($entity) ? $entity : \get_class($entity)),
$this->configuration->getTableSuffix(),
]);

/**
* @var \Doctrine\DBAL\Query\QueryBuilder
*/
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder
->select('*')
->from($auditTable)
->from($this->getEntityAuditTableName($entity))
->where('id = :id')
->setParameter('id', $id);

Expand All @@ -279,6 +264,13 @@ public function getAudit($entity, $id)
return $statement->fetchAll();
}

private function getClassMetadata($entity): ClassMetadata
{
return $this
->entityManager
->getClassMetadata($entity);
}

/**
* Returns the table name of $entity.
*
Expand All @@ -288,9 +280,22 @@ public function getAudit($entity, $id)
*/
public function getEntityTableName($entity): string
{
return $this
->entityManager
->getClassMetadata($entity)
return $this->getClassMetadata($entity)
->getTableName();
}

/**
* Returns the audit table name for $entity
*
* @param $entity
*
* @return string
*/
public function getEntityAuditTableName($entity): string
{
$entityName = \is_string($entity) ? $entity : \get_class($entity);
$schema = $this->getClassMetadata($entityName)->getSchemaName() ? $this->getClassMetadata($entityName)->getSchemaName() . '.' : '';

return sprintf('%s%s%s%s', $schema, $this->configuration->getTablePrefix(), $this->getEntityTableName($entityName), $this->configuration->getTableSuffix());
}
}
17 changes: 17 additions & 0 deletions tests/DoctrineAuditBundle/Reader/AuditReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ public function testGetEntityTableName(): void
$this->assertSame('comment', $reader->getEntityTableName(Comment::class), 'tablename is ok.');
}

public function testGetEntityTableAuditName(): void
{
$entities = [
Post::class => null,
Comment::class => null,
];

$configuration = $this->createAuditConfiguration([
'entities' => $entities,
]);

$reader = $this->getReader($configuration);

$this->assertSame('post_audit', $reader->getEntityAuditTableName(Post::class), 'tablename is ok.');
$this->assertSame('comment_audit', $reader->getEntityAuditTableName(Comment::class), 'tablename is ok.');
}

/**
* @depends testGetEntityTableName
*/
Expand Down

0 comments on commit f7a59e8

Please sign in to comment.