Skip to content

Commit

Permalink
AuditReader::getAudits() and AuditController::showEntityHistoryAction…
Browse files Browse the repository at this point in the history
…() do not return the first 50 history items anymore but the full history. Though, it is still possible to get only a few items by specifying $pageSize and $page arguments
  • Loading branch information
DamienHarper committed Apr 12, 2019
1 parent 257c8cc commit dcdb0ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/DoctrineAuditBundle/Controller/AuditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function listAuditsAction(): Response
*
* @param string $entity
* @param int|string $id
* @param int $page
* @param int $pageSize
* @param null|int $page
* @param null|int $pageSize
*
* @return Response
*/
public function showEntityHistoryAction(string $entity, $id = null, int $page = 1, int $pageSize = 50): Response
public function showEntityHistoryAction(string $entity, $id = null, ?int $page = null, ?int $pageSize = null): Response
{
$reader = $this->container->get('dh_doctrine_audit.reader');
$entries = $reader->getAudits($entity, $id, $page, $pageSize);
Expand Down
21 changes: 14 additions & 7 deletions src/DoctrineAuditBundle/Reader/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,18 @@ public function getEntities(): array
*
* @param object|string $entity
* @param int|string $id
* @param int $page
* @param int $pageSize
* @param null|int $page
* @param null|int $pageSize
*
* @return array
*/
public function getAudits($entity, $id = null, int $page = 1, int $pageSize = 50): array
public function getAudits($entity, $id = null, ?int $page = null, ?int $pageSize = null): array
{
if ($page < 1) {
if (null !== $page && $page < 1) {
throw new \InvalidArgumentException('$page must be greater or equal than 1.');
}
if ($pageSize < 1) {

if (null !== $pageSize && $pageSize < 1) {
throw new \InvalidArgumentException('$pageSize must be greater or equal than 1.');
}

Expand All @@ -137,8 +138,14 @@ public function getAudits($entity, $id = null, int $page = 1, int $pageSize = 50
->from($auditTable)
->orderBy('created_at', 'DESC')
->addOrderBy('id', 'DESC')
->setFirstResult(($page - 1) * $pageSize)
->setMaxResults($pageSize);
;

if (null !== $pageSize) {
$queryBuilder
->setFirstResult(($page - 1) * $pageSize)
->setMaxResults($pageSize)
;
}

if (null !== $id) {
$queryBuilder
Expand Down

0 comments on commit dcdb0ce

Please sign in to comment.