From 35f1a4b6722f076f7d60d4a30caf6890a0230da4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 4 Jul 2016 08:57:37 +0200 Subject: [PATCH] Created stuff to handle pagination on list results --- composer.json | 1 + src/Repository/PaginableRepository.php | 14 +++++++ src/Repository/ShortUrlRepository.php | 42 +++++++++++++++++++ .../ShortUrlRepositoryInterface.php | 8 ++++ src/Service/ShortUrlService.php | 3 +- src/Service/ShortUrlServiceInterface.php | 3 +- src/Service/VisitsTracker.php | 3 +- src/Service/VisitsTrackerInterface.php | 3 +- 8 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/Repository/PaginableRepository.php create mode 100644 src/Repository/ShortUrlRepository.php create mode 100644 src/Repository/ShortUrlRepositoryInterface.php diff --git a/composer.json b/composer.json index 15f92bee..374fed26 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "zendframework/zend-expressive-twigrenderer": "^1.0", "zendframework/zend-stdlib": "^2.7", "zendframework/zend-servicemanager": "^3.0", + "zendframework/zend-paginator": "^2.6", "doctrine/orm": "^2.5", "guzzlehttp/guzzle": "^6.2", "acelaya/zsm-annotated-services": "^0.2.0", diff --git a/src/Repository/PaginableRepository.php b/src/Repository/PaginableRepository.php new file mode 100644 index 00000000..ad993961 --- /dev/null +++ b/src/Repository/PaginableRepository.php @@ -0,0 +1,14 @@ +createQueryBuilder('s'); + + if (isset($limit)) { + $qb->setMaxResults($limit); + } + if (isset($offset)) { + $qb->setFirstResult($offset); + } + if (isset($searchTerm)) { + // TODO + } + if (isset($orderBy)) { + if (is_string($orderBy)) { + $qb->orderBy($orderBy); + } elseif (is_array($orderBy)) { + $key = key($orderBy); + $qb->orderBy($key, $orderBy[$key]); + } + } else { + $qb->orderBy('s.dateCreated'); + } + + return $qb->getQuery()->getResult(); + } +} diff --git a/src/Repository/ShortUrlRepositoryInterface.php b/src/Repository/ShortUrlRepositoryInterface.php new file mode 100644 index 00000000..37b013d9 --- /dev/null +++ b/src/Repository/ShortUrlRepositoryInterface.php @@ -0,0 +1,8 @@ +