Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,34 @@ component accessors="true" transientCache="false" {
return true;
}

/**
* Retrieves the configured query in chunks and passes hydrated entities to
* the callback. Returning false from the callback stops further retrieval.
*
* @max The maximum number of entities in each chunk.
* @callback The callback invoked for each entity collection.
* @options Any options to pass to `queryExecute`. Default: {}.
*
* @return quick.models.QuickBuilder
*/
public any function chunk(
required numeric max,
required callback,
struct options = {}
) {
activateGlobalScopes();
variables.qb.chunk(
arguments.max,
function( rows ) {
var entities = variables._asQuery ? arguments.rows : arguments.rows.map( variables.loadEntity );
var collection = getEntity().newCollection( handleTransformations( eagerLoadRelations( entities ) ) );
return callback( collection );
},
arguments.options
);
return this;
}

/**
* Returns a Pagination Collection of entities.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/specs/integration/BaseEntity/GetSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( p.results[ p.results.len() ].getId() ).toBe( 45 );
} );

it( "can retrieve entities in chunks", function() {
var chunks = [];
var query = getInstance( "User" )
.orderBy( "id" )
.chunk( 2, function( users ) {
chunks.append( {
"size" : users.len(),
"firstId" : users[ 1 ].getId(),
"quickEntity" : users[ 1 ].isQuickEntity
} );
return chunks.len() < 2;
} );

expect( query.isQuickBuilder ).toBeTrue();
expect( chunks ).toHaveLength( 2 );
expect( chunks[ 1 ] ).toBe( {
"size" : 2,
"firstId" : 1,
"quickEntity" : true
} );
expect( chunks[ 2 ] ).toBe( {
"size" : 2,
"firstId" : 3,
"quickEntity" : true
} );
} );

it( "can eager load and paginate a Quick query", function() {
queryExecute( "TRUNCATE TABLE `a`" );
queryExecute( "TRUNCATE TABLE `b`" );
Expand Down
Loading