Skip to content

Commit

Permalink
fix(BaseEntity): Fixed issue with NULL values being inserted into ide…
Browse files Browse the repository at this point in the history
…ntity columns for MSSQL
  • Loading branch information
ryanalbrecht authored Jun 12, 2024
1 parent cab3727 commit 4ae80fa
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
22 changes: 15 additions & 7 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ component accessors="true" {
/**
* An array of virtual attribute key names that have been add to this entity
*/
property name="_virtualAttributes" persistent="false";
property name="_virtualAttributes" persistent="false";


/**
Expand Down Expand Up @@ -1094,7 +1094,7 @@ component accessors="true" {
var parent = variables._wirebox.getInstance( parentDefinition.meta.fullName );
}

parent.fill( getMemento(), true ).save();
parent.fill( retrieveAttributesData(), true ).save();

assignAttributesData( {
"#parentDefinition.key#" : parent.keyValues()[ 1 ],
Expand Down Expand Up @@ -1166,10 +1166,14 @@ component accessors="true" {
fireEvent( "postSave", { entity : this } );

// re-cast
for ( var key in variables._castCache) {
var castedValue = castValueForGetter( key, variables._castCache[ key ], true );
for ( var key in variables._castCache ) {
var castedValue = castValueForGetter(
key,
variables._castCache[ key ],
true
);
variables._data[ retrieveColumnForAlias( key ) ] = castedValue;
variables[ retrieveAliasForColumn( key ) ] = castedValue;
variables[ retrieveAliasForColumn( key ) ] = castedValue;
}

return this;
Expand Down Expand Up @@ -3264,10 +3268,14 @@ component accessors="true" {
*
* @return any
*/
private any function castValueForGetter( required string key, any value, boolean forceCast = false ) {
private any function castValueForGetter(
required string key,
any value,
boolean forceCast = false
) {
arguments.key = retrieveAliasForColumn( arguments.key );

if ( structKeyExists( variables._castCache, arguments.key ) AND !arguments.forceCast ) {
if ( structKeyExists( variables._castCache, arguments.key ) AND !arguments.forceCast ) {
return variables._castCache[ arguments.key ];
}

Expand Down
4 changes: 1 addition & 3 deletions models/Casts/JsonCast.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ component singleton {
required string key,
any value
) {

if ( isNull( arguments.value ) ) {
return javacast( "null", "" );
}

if( isJSON( arguments.value ) ){
if ( isJSON( arguments.value ) ) {
return deserializeJSON( arguments.value );
}

return arguments.value;

}

/**
Expand Down
10 changes: 6 additions & 4 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1311,10 +1311,12 @@ component accessors="true" transientCache="false" {
].mapping
);

//add any virtual attributes present in the parent entity to child entity
getEntity().get_virtualAttributes().each( function( item ) {
childClass.appendVirtualAttribute( item );
} );
// add any virtual attributes present in the parent entity to child entity
getEntity()
.get_virtualAttributes()
.each( function( item ) {
childClass.appendVirtualAttribute( item );
} );

return childClass
.assignAttributesData( arguments.data )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ component {
t.increments( "id" );
t.string( "title" ).nullable();
t.string( "download_url" );
t.timestamp( "created_date" ).withCurrent();
t.timestamp( "modified_date" ).withCurrent();

t.raw( "`created_date` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)" );
t.raw( "`modified_date` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)" );

//proposed qb pull request https://github.com/coldbox-modules/qb/pull/282:
//t.timestamp( "created_date", 6 ).withCurrent( 6 );
//t.timestamp( "modified_date", 6 ).withCurrent( 6 );
} );

qb.table( "songs" ).insert( [
Expand Down
13 changes: 4 additions & 9 deletions tests/specs/integration/BaseEntity/AttributeCastsSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,28 @@ component extends="tests.resources.ModuleIntegrationSpec" {
} );

it( "can maintain casts when loading a discriminated child through the parent", () => {
var comment = getInstance( "Comment" )
.where('designation', 'internal')
.first();
var comment = getInstance( "Comment" ).where( "designation", "internal" ).first();

expect( comment.getSentimentAnalysis() ).notToBeNull();
expect( comment.getSentimentAnalysis() ).toBeStruct();
expect( comment.getSentimentAnalysis() ).toHaveKey( "analyzed" );
expect( comment.getSentimentAnalysis() ).toHaveKey( "magnitude" );
expect( comment.getSentimentAnalysis() ).toHaveKey( "score" );

} );

it( "will recast after saving entity", function() {
var pn = getInstance( "PhoneNumber" ).find(1);
var pn = getInstance( "PhoneNumber" ).find( 1 );
pn.setNumber( "111-111-1111" );
pn.setActive( "0" );

expect( pn.getActive() ).toBe( "0" );
expect( pn.getActive() ).toBeNumeric();

pn.save();

expect( pn.getActive() ).toBe( false );
expect( pn.getActive() ).toBe( false );
expect( pn.getActive() ).toBeBoolean();

} );

} );
}

Expand Down
7 changes: 4 additions & 3 deletions tests/specs/integration/BaseEntity/AttributeHashSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ component extends="tests.resources.ModuleIntegrationSpec" {
} );

it( "can compute attributes hash correctly", function() {
var user = getInstance( "User" ).populate(
var passwordHash = hash( "password" )
var user = getInstance( "User" ).populate(
{
"username" : "JaneDoe",
"first_name" : "Jane",
"last_name" : "Doe",
"password" : hash( "password" ),
"password" : passwordHash,
"non-existant-property" : "any-value"
},
true
);

var expectedHash = hash( "first_name=Jane&last_name=Doe&password=5F4DCC3B5AA765D61D8327DEB882CF99&username=JaneDoe" );
var expectedHash = hash( "first_name=Jane&last_name=Doe&password=#passwordHash#&username=JaneDoe" );
var hash = user.computeAttributesHash( user.retrieveAttributesData() );
expect( expectedHash ).toBe( hash, "The computeAttributesHash method does not return the correct hash" );
} );
Expand Down
3 changes: 1 addition & 2 deletions tests/specs/integration/BaseEntity/ChildClassSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,12 @@ component extends="tests.resources.ModuleIntegrationSpec" {
var comment = getInstance( "Comment" )
.addUpperBody()
.where( "designation", "internal" )
.get()[1]
.get()[ 1 ]

expect( comment.hasAttribute( "upperBody" ) ).toBeTrue(
"Child class should have a virtual attribute 'upperBody'."
);
} );

} );

describe( "Single Table Inheritence Class Spec", function() {
Expand Down

0 comments on commit 4ae80fa

Please sign in to comment.