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
53 changes: 24 additions & 29 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,28 @@ component accessors="true" transientCache="false" {
variables.qb.setWheres( wheres );
}

/**
* Adds grouped primary-key constraints for bulk delete operations.
*/
private void function addIdConstraints( required array ids ) {
if ( arrayIsEmpty( arguments.ids ) ) {
return;
}

var idConstraints = variables.qb.forNestedWhere();
var keyNames = getEntity().keyNames();
for ( var id in arguments.ids ) {
var values = arrayWrap( id );
getEntity().guardAgainstKeyLengthMismatch( values );
var keyConstraints = idConstraints.forNestedWhere();
for ( var i = 1; i <= keyNames.len(); i++ ) {
keyConstraints.where( keyNames[ i ], values[ i ] );
}
idConstraints.addNestedWhereQuery( keyConstraints, "or" );
}
variables.qb.addNestedWhereQuery( idConstraints );
}

/**
* Deletes matching entities according to the configured query.
*
Expand All @@ -687,20 +709,7 @@ component accessors="true" transientCache="false" {
*/
public struct function deleteAll( array ids = [] ) {
getEntity().guardReadOnly();
if ( !arrayIsEmpty( arguments.ids ) ) {
var idConstraints = variables.qb.forNestedWhere();
for ( var id in arguments.ids ) {
var values = arrayWrap( id );
getEntity().guardAgainstKeyLengthMismatch( values );
var keyConstraints = idConstraints.forNestedWhere();
var keyNames = getEntity().keyNames();
for ( var i = 1; i <= keyNames.len(); i++ ) {
keyConstraints.where( keyNames[ i ], values[ i ] );
}
idConstraints.addNestedWhereQuery( keyConstraints, "or" );
}
variables.qb.addNestedWhereQuery( idConstraints );
}
addIdConstraints( arguments.ids );
if ( getEntity().usesSoftDeletes() ) {
activateGlobalScopes();
return updateAll( { "#getEntity().retrieveSoftDeleteColumn()#" : now() } );
Expand All @@ -727,21 +736,7 @@ component accessors="true" transientCache="false" {
*/
public struct function forceDeleteAll( array ids = [] ) {
getEntity().guardReadOnly();
if ( !arrayIsEmpty( arguments.ids ) ) {
variables.qb.where( function( q1 ) {
ids.each( function( id ) {
var values = arrayWrap( id );
getEntity().guardAgainstKeyLengthMismatch( values );
q1.orWhere( function( q2 ) {
getEntity()
.keyNames()
.each( function( keyName, i ) {
q2.where( keyName, values[ i ] );
} );
} );
} );
} );
}
addIdConstraints( arguments.ids );
return variables.qb.delete();
}

Expand Down
8 changes: 8 additions & 0 deletions tests/specs/integration/BaseEntity/SoftDeletesSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ component extends="tests.resources.ModuleIntegrationSpec" {
trashedUser.forceDelete();
expect( getInstance( "SoftDeleteUser" ).withTrashed().find( 1 ) ).toBeNull();
} );

it( "can force delete multiple entities by id", function() {
getInstance( "SoftDeleteUser" ).forceDeleteAll( [ 1, 2 ] );

expect( getInstance( "SoftDeleteUser" ).withTrashed().find( 1 ) ).toBeNull();
expect( getInstance( "SoftDeleteUser" ).withTrashed().find( 2 ) ).toBeNull();
expect( getInstance( "SoftDeleteUser" ).findOrFail( 3 ).getUsername() ).toBe( "janedoe" );
} );
} );
}

Expand Down
Loading