Streaming Results

You can load records one at a time to enable exporting large amounts of data to a file.

If you need to export a large amount of data, and want to fetch and hydrate one record at a time (so as not to run out of memory), you can ask for an iterable result by calling findOnDemandBy. Instead of getting back an array of entities, you will get an instance of the IterableResult class - which implements PHP's \Iterator interface. Each iteration of the result will fetch and hydrate a new record from the database (keeping the database connection open throughout) - just like using fetch with PDO. As the query is kept open during iteration, it is not possible to lazy load or late bind child objects with an iterable result, so lazy loading becomes disabled, and one-to-many and many-to-many relationships cannot be hydrated.

If you are exporting records to a flat file (such as a CSV), it is unlikely that you will need an object model to work with, so there is no point mapping data from the database into objects and then converting them back into an array for export, so it is usually best to call $repository->setConfigOption(ConfigOptions::BIND_TO_ENTITIES, false) before requesting an iterable result (the results that you iterate through will then be associative arrays).

Last updated