Skip to content
Merged
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
33 changes: 33 additions & 0 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ component accessors="true" transientCache="false" {
return this;
}

private void function ensureKeyColumnsSelected() {
var selectedColumns = variables.qb.getColumns();
if (
selectedColumns.some( function( column ) {
return column.type == "simple" && column.value.find( "*" );
} )
) {
return;
}

for ( var keyColumn in getEntity().keyColumns() ) {
var qualifiedKey = getEntity().qualifyColumn( keyColumn );
var hasKey = selectedColumns.some( function( column ) {
return column.type == "simple" && compareNoCase( column.value, qualifiedKey ) == 0;
} );
if ( !hasKey ) {
variables.qb.addSelect( qualifiedKey );
}
}
}

/**
* Adds a subselect query with the given name to the entity.
* Useful for computed properties and computed relationship keys.
Expand Down Expand Up @@ -392,6 +413,9 @@ component accessors="true" transientCache="false" {
* @return [quick.models.BaseEntity]
*/
private array function getEntities( any columns, struct options = {} ) {
if ( !variables._asQuery ) {
ensureKeyColumnsSelected();
}
var results = variables.qb.get( argumentCollection = arguments );
return variables._asQuery ? results : results.map( variables.loadEntity );
}
Expand Down Expand Up @@ -1091,6 +1115,9 @@ component accessors="true" transientCache="false" {
*/
public any function first( struct options = {} ) {
activateGlobalScopes();
if ( !variables._asQuery ) {
ensureKeyColumnsSelected();
}

var result = variables.qb.first( argumentCollection = arguments );
return structIsEmpty( result ) ? javacast( "null", "" ) : handleTransformations(
Expand Down Expand Up @@ -1319,6 +1346,9 @@ component accessors="true" transientCache="false" {
struct options = {}
) {
activateGlobalScopes();
if ( !variables._asQuery ) {
ensureKeyColumnsSelected();
}
var p = variables.qb.paginate(
arguments.page,
arguments.maxRows,
Expand Down Expand Up @@ -1346,6 +1376,9 @@ component accessors="true" transientCache="false" {
struct options = {}
) {
activateGlobalScopes();
if ( !variables._asQuery ) {
ensureKeyColumnsSelected();
}
var p = variables.qb.simplePaginate(
arguments.page,
arguments.maxRows,
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/integration/BaseEntity/ColumnsSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( bindings[ 1 ] ).toBeStruct();
expect( bindings[ 1 ].value ).toBe( "firstName" );
} );

it( "preserves the entity key when selecting specific columns", function() {
var user = getInstance( "User" ).select( "username" ).findOrFail( 1 );

expect( user.getId() ).toBe( 1 );
expect( user.getUsername() ).toBe( "elpete" );
} );

it( "preserves every composite key column when selecting specific columns", function() {
var composite = getInstance( "Composite" ).select( "a" ).findOrFail( [ 1, 2 ] );

expect( composite.keyValues() ).toBe( [ 1, 2 ] );
} );
} );
}

Expand Down
Loading