diff --git a/models/BaseEntity.cfc b/models/BaseEntity.cfc index a3f7a943..b409f8ba 100644 --- a/models/BaseEntity.cfc +++ b/models/BaseEntity.cfc @@ -1314,6 +1314,20 @@ component accessors="true" { return save(); } + /** + * Updates a timestamp attribute to the current time and saves the entity. + * + * @attribute The timestamp attribute to update. Default: `modifiedDate`. + * @options Any options to pass to `queryExecute`. Default: {}. + * + * @return quick.models.BaseEntity + */ + public any function touch( string attribute = "modifiedDate", struct options = {} ) { + guardAgainstNotLoaded( "This instance is not loaded so it cannot be touched." ); + assignAttribute( arguments.attribute, now() ); + return save( arguments.options ); + } + /** * Creates a new entity with the given attributes and then saves the entity. * diff --git a/tests/specs/integration/BaseEntity/SaveSpec.cfc b/tests/specs/integration/BaseEntity/SaveSpec.cfc index 61a9d2ef..10d6ca56 100644 --- a/tests/specs/integration/BaseEntity/SaveSpec.cfc +++ b/tests/specs/integration/BaseEntity/SaveSpec.cfc @@ -76,6 +76,17 @@ component extends="tests.resources.ModuleIntegrationSpec" { expect( userRowsPostSave ).toHaveLength( 5 ); } ); + it( "can touch an entity timestamp", function() { + var user = getInstance( "User" ).findOrFail( 1 ); + var originalModified = user.getModifiedDate(); + + user.touch(); + + expect( dateCompare( user.getModifiedDate(), originalModified ) ).toBe( 1 ); + expect( user.isDirty( "modifiedDate" ) ).toBeFalse(); + expect( dateCompare( user.fresh().getModifiedDate(), originalModified ) ).toBe( 1 ); + } ); + it( "does not allow updating of column where update=false in property", function() { var existingUser = getInstance( "User" ).find( 1 ); existingUser.setEmail( "test2@test.com" );