A plugin behavior to find all records in batches and apply a callback
Switch branches/tags
Nothing to show
Failed to load latest commit information.
models/behaviors Unsetting inappropriate fields for the count query. Jun 22, 2011
tests Initial import Mar 10, 2011
README.markdown Initial import Mar 10, 2011


What is it?

It's a plugin behavior that provides a way of finding all records in batches rather than all at once, so that you can loop through huge lists of records without running out of memory.

Rather than returning any results, it calls a callback function you provide and passes the current bacth of results to it, along with a $batchInfo array containing the current range and total record count.

It accepts the same array of options as a normal find('all') does. The limit option is used to set the batch size.


Add this to your model, or your AppModel, or even bind at runtime:

class MyModel extends AppModel {
	var $actsAs = array('FindBatch.FindBatch');


PHP 5.3+ style with an anonymous function as a callback

	'limit' => 5,
	'order' => array('title'=>'asc')
), function($results, $batchInfo) {

Old-skool with an array-style callback

	'limit' => 5,
	'order' => array('title'=>'asc')
), array($this,'callbackMethodName'));

function callBackMethodName($results, $batchInfo) {

Suggested callback

For anonymous functions, pass in external variables by reference with a use() clause in the function header if necessary.

$self = $this;

function ($results, $batchInfo) use(&$self, &$someVariable) {
	// Batch info is available like so:
	echo "These are records {$batchInfo['start']} to {$batchInfo['end']} of {$batchInfo['totalRecords']} ... \n";
	foreach($results as $result) {
		// do your processing here