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
41 changes: 40 additions & 1 deletion models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ component accessors="true" {
} ),
arguments.options
);
refreshAttributesOnSave();
assignOriginalAttributes( retrieveAttributesData() );
markLoaded();
fireEvent(
Expand Down Expand Up @@ -1217,8 +1218,9 @@ component accessors="true" {
result.result[ getParentDefinition().joincolumn ] = variables._data[ getParentDefinition().joinColumn ];
}
retrieveKeyType().postInsert( this, result );
assignOriginalAttributes( retrieveAttributesData() );
markLoaded();
refreshAttributesOnSave();
assignOriginalAttributes( retrieveAttributesData() );
fireEvent(
"postInsert",
{
Expand Down Expand Up @@ -1250,6 +1252,39 @@ component accessors="true" {
return this;
}

/**
* Refreshes attributes whose values are generated or changed by the database
* during persistence.
*/
private void function refreshAttributesOnSave() {
var attributesToRefresh = variables._attributes.filter( function( name, attribute ) {
return attribute.refreshOnSave;
} );

if ( attributesToRefresh.isEmpty() ) {
return;
}

var refreshedEntity = newQuery()
.withoutGlobalScope()
.where( function( q ) {
arrayZipEach( [ keyNames(), keyValues() ], function( keyName, keyValue ) {
q.where( keyName, keyValue );
} );
} )
.first();
if ( isNull( refreshedEntity ) ) {
return;
}

var refreshedData = refreshedEntity.retrieveAttributesData( withNulls = true );
attributesToRefresh.each( function( name, attribute ) {
var value = refreshedData[ attribute.column ];
variables._data[ attribute.column ] = isNull( value ) ? javacast( "null", "" ) : value;
variables[ attribute.name ] = isNull( value ) ? javacast( "null", "" ) : value;
} );
}

/**
* Deletes the entity from the database.
* This function can only be called on loaded entities.
Expand Down Expand Up @@ -2973,12 +3008,16 @@ component accessors="true" {
param attr.sqltype = "";
param attr.insert = true;
param attr.update = true;
param attr.refreshOnSave = false;
param attr.virtual = false;
param attr.exclude = false;
param attr.isParentColumn = false;
if ( !isBoolean( attr.persistent ) ) {
attr.persistent = lCase( trim( attr.persistent & "" ) ) == "true";
}
if ( !isBoolean( attr.refreshOnSave ) ) {
attr.refreshOnSave = lCase( trim( attr.refreshOnSave & "" ) ) == "true";
}
variables._nullValues[ attr.name ] = attr.nullValue;
return arguments.attr;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/resources/app/models/DatabaseGeneratedUser.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component extends="quick.models.BaseEntity" accessors="true" table="users" {

property name="id";
property name="username";
property name="firstName" column="first_name";
property name="lastName" column="last_name";
property name="createdDate" column="created_date" refreshOnSave="true";

}
12 changes: 12 additions & 0 deletions tests/specs/integration/BaseEntity/SaveSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( newUser.retrieveAttributesData() ).toHaveKey( "id" );
} );

it( "retrieves database-generated attributes marked to refresh on save", function() {
var newUser = getInstance( "DatabaseGeneratedUser" )
.setUsername( "database-timestamp-user" )
.setFirstName( "Database" )
.setLastName( "Timestamp" )
.save();

expect( newUser.getCreatedDate() ).notToBe( "" );
expect( newUser.getCreatedDate() ).toBeDate();
expect( newUser.isDirty( "createdDate" ) ).toBeFalse();
} );

it( "a saved entity is not dirty", function() {
var newUser = getInstance( "User" );
newUser.setUsername( "new_user" );
Expand Down
Loading