Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with NULL values being inserted into identity columns for MSSQL #250

Merged
merged 3 commits into from
Jun 12, 2024
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
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
Loading