1. Go to this page and download the library: Download paysera/lib-pagination library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paysera / lib-pagination example snippets
use Paysera\Pagination\Entity\Doctrine\ConfiguredQuery;
use Paysera\Pagination\Service\Doctrine\ResultProvider;
use Paysera\Pagination\Service\CursorBuilder;
use Paysera\Pagination\Service\Doctrine\QueryAnalyser;
use Paysera\Pagination\Entity\OrderingConfiguration;
use Paysera\Pagination\Entity\Pager;
use Paysera\Pagination\Entity\OrderingPair;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\EntityManagerInterface;
$resultProvider = new ResultProvider(
new QueryAnalyser(),
new CursorBuilder(PropertyAccess::createPropertyAccessor())
);
/** @var EntityManagerInterface $entityManager */
$queryBuilder = $entityManager->createQueryBuilder()
->select('m')
->from('Bundle:MyEntity', 'm')
->andWhere('m.field = :param')
->setParameter('param', 'some value')
;
$configuredQuery = (new ConfiguredQuery($queryBuilder))
->addOrderingConfiguration(
'my_field_name',
(new OrderingConfiguration('m.field', 'field'))->setOrderAscending(true)
)
->addOrderingConfiguration('my_other_name', new OrderingConfiguration('m.otherField', 'otherField'))
->setTotalCountNeeded(true) // total count will be returned only if this is called
->setMaximumOffset(100) // you can optionally limit maximum offset
->setItemTransformer(function ($item) {
// return transformed item if needed
})
;
$pager = (new Pager())
->setLimit(10)
->setOffset(123) // set only one of offset, after or before
->setAfter('Cursor from Result::getNextCursor')
->setBefore('Cursor from Result::getPreviousCursor')
->addOrderBy(new OrderingPair('my_field_name')) // order by default direction (asc in this case)
->addOrderBy(new OrderingPair('my_other_name', true)) // or set direction here
;
$result = $resultProvider->getResultForQuery($configuredQuery, $pager);
$result->getItems(); // items in the page
$result->getNextCursor(); // value to pass with setAfter for next page
$result->getPreviousCursor(); // value to pass with setBefore for previous page
$result->getTotalCount(); // available only if setTotalCountNeeded(true) was called
$totalCount = $resultProvider->getTotalCountForQuery($configuredQuery); // calculate total count directly
use Paysera\Pagination\Entity\Doctrine\ConfiguredQuery;
use Paysera\Pagination\Service\Doctrine\ResultIterator;
use Paysera\Pagination\Service\CursorBuilder;
use Paysera\Pagination\Service\Doctrine\QueryAnalyser;
use Paysera\Pagination\Service\Doctrine\ResultProvider;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Psr\Log\NullLogger;
/** @var ConfiguredQuery $configuredQuery */
$resultIterator = new ResultIterator(
new ResultProvider(
new QueryAnalyser(),
new CursorBuilder(PropertyAccess::createPropertyAccessor())
),
new NullLogger(),
$defaultLimit = 1000
);
foreach ($this->resultIterator->iterate($configuredQuery) as $item) {
// process $item where flush is not needed
// for example, send ID or other data to working queue, process files etc.
}
use Paysera\Pagination\Entity\Doctrine\ConfiguredQuery;
use Paysera\Pagination\Service\Doctrine\FlushingResultIterator;
use Paysera\Pagination\Service\CursorBuilder;
use Paysera\Pagination\Service\Doctrine\QueryAnalyser;
use Paysera\Pagination\Service\Doctrine\ResultProvider;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Psr\Log\NullLogger;
use Doctrine\ORM\EntityManagerInterface;
/** @var ConfiguredQuery $configuredQuery */
/** @var EntityManagerInterface $entityManager */
$resultIterator = new FlushingResultIterator(
new ResultProvider(
new QueryAnalyser(),
new CursorBuilder(PropertyAccess::createPropertyAccessor())
),
new NullLogger(),
$defaultLimit = 1000,
$entityManager
);
foreach ($this->resultIterator->iterate($configuredQuery) as $item) {
// process $item or other entities where flush will be called after each page
// for example:
$item->setTitle(formatTitleFor($item));
// keep in mind, that clear is also called – don't reuse other Entities outside of foreach cycle
}
echo "Updated successfully";
// no need to flush here anymore
$lastCursor = '"123"'; // get from logs
$startPager = (new Pager())
->setLimit(500) // can also override default limit
->setAfter($lastCursor)
;
foreach ($this->resultIterator->iterate($configuredQuery, $startPager) as $item) {
// process $item
}
sql
SELECT *
FROM items
WHERE field = 'some value'
AND created_at <= '2018-01-01 00:01:02' AND id < 12345
ORDER BY created_at DESC, id DESC
LIMIT 100
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.