Skip to content
Batch allows you to separate code that is fetching data and processing them in batches. It has many uses.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Rixxi/Utils
composer.json
license.md
readme.md

readme.md

Batch allows you to separate code that is fetching data and processing them in batches. It has many uses.

Example

Write ten lines of 80 characters per line.

use Rixxi\Utils\Batch;

$batch = new Batch(function ($values) {
	echo implode($values), PHP_EOL;
}, $limit = 80);

$lines = 10;
for ($x = $limit * $lines; $x-- > 0;) {
	$batch[] = chr(ord('A') + rand(0, 26 /* ord('Z') - ord('A') */));
}

Flushing & Callback

Batch is automatically flushed and reset when limit is reached and when is destroyed.

If no items were added to batch since start or last flush callback is not called.

You can flush batch manually at any point calling flush().

$batch->flush();

Or flush and throw away calling unset on batch.

unset($batch);

Usually you don't have to unset since garbage collector will do it for you at the end of function.

Install

composer require rixxi/utils

Troubleshooting

If you are getting random errors after processing data via Batch make sure you flush it before doing anything else. You are probably assuming all data are flushed when there might be few left after last round.

Ie.: in Doctrine when processing entities don't forget to flush manually before calling $repository->clear();.

You can’t perform that action at this time.