Download the PHP package wiistriker/doctrine-cursor-iterator without Composer
On this page you can find all versions of the php package wiistriker/doctrine-cursor-iterator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wiistriker/doctrine-cursor-iterator
More information about wiistriker/doctrine-cursor-iterator
Files in wiistriker/doctrine-cursor-iterator
Package doctrine-cursor-iterator
Short Description Iterate through large datasets
License MIT
Informations about the package doctrine-cursor-iterator
Doctrine ORM and DBAL Cursor Paginator for large datasets
Iterate through large database results with easy
Installation
Usage
Both Doctrine ORM and DBAL query builders are supported:
- Usage for ORM
- Usage for DBAL
Usage for ORM
Create query builder as usual. Dont forget about orderBy and maxResults.
DoctrineORMCursorPaginator fetches only 100 records per query, so it never loads the whole dataset into memory at once and can efficiently iterate through even large datasets. See Memory and the EntityManager below for an important caveat about object hydration.
First sql:
Next:
You can also specify more order by fields
You can change hydration mode
And even set query hints
You wanna batch? Lets batch:
By default batch size equals to maxResults but you can also specify desired amount by yourself:
Memory and the EntityManager
The paginator limits how many rows each query returns, but with the default object hydration
(HYDRATE_OBJECT) Doctrine keeps every hydrated entity in the EntityManager's identity map. Over a large
dataset that map keeps growing, so the per-query limit alone does not keep memory flat. When you iterate
over many entities, clear the EntityManager periodically (batching makes a natural place to do it):
Keep in mind that clear() detaches all managed entities: flush any pending changes before calling it,
and don't keep references to entities you still expect to be managed. If you don't need managed objects at all,
array hydration avoids the identity map entirely and sidesteps the issue:
Usage for DBAL
Just use DoctrineDBALCursorPaginator instead.
DBAL exposes no public getter for the ORDER BY clause, so by default the order
is read from the query builder via reflection. If you prefer to avoid reflection
(or your DBAL version changes its internals), pass the order explicitly. It must
mirror the orderBy()/addOrderBy() calls on the query builder: