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
14 changes: 14 additions & 0 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,20 @@ component accessors="true" {
relationship.relationshipClass != "BelongsTo" &&
relationship.relationshipClass != "PolymorphicBelongsTo"
) {
if ( !isLoaded() ) {
var relationshipValue = arguments.missingMethodArguments[ 1 ];
var relatedEntity = relationship.getRelated();
var fillRelatedEntity = function( value ) {
return isStruct( arguments.value ) && !structKeyExists( arguments.value, "isQuickEntity" )
? relatedEntity.newEntity().fill( arguments.value )
: arguments.value;
};
var filledRelationship = isArray( relationshipValue )
? relationshipValue.map( fillRelatedEntity )
: fillRelatedEntity( relationshipValue );
assignRelationship( relationshipName, filledRelationship );
return filledRelationship;
}
guardAgainstNotLoaded(
"This instance is not loaded so it cannot set the [#relationshipName#] relationship. " &
"Save the new entity first before trying to save related entities."
Expand Down
19 changes: 19 additions & 0 deletions tests/specs/integration/BaseEntity/CreateSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ component extends="tests.resources.ModuleIntegrationSpec" {
).notToBeNull();
} );

it( "creates only the root while retaining filled relationships in memory", function() {
var user = getInstance( "User" ).create( {
"username" : "aggregate-user",
"first_name" : "Aggregate",
"last_name" : "User",
"password" : hash( "password" ),
"posts" : [
{ "body" : "First child" },
{ "body" : "Second child" }
]
} );

expect( user.isLoaded() ).toBeTrue();
expect( user.getPosts() ).toHaveLength( 2 );
expect( user.getPosts()[ 1 ].isLoaded() ).toBeFalse();
expect( user.getPosts()[ 2 ].isLoaded() ).toBeFalse();
expect( user.fresh().getPosts() ).toBeEmpty();
} );

it( "can create a new entity with a json cast", () => {
var newTheme = getInstance( "Theme" ).create( {
slug : "theme-new",
Expand Down
18 changes: 18 additions & 0 deletions tests/specs/integration/BaseEntity/FillSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ component extends="tests.resources.ModuleIntegrationSpec" {
} ).notToThrow();
expect( user.isNullAttribute( "updatedDate" ) ).toBeTrue();
} );

it( "can fill relationships on a new entity without persisting the aggregate", function() {
var user = getInstance( "User" ).fill( {
"posts" : [
getInstance( "Post" ).fill( { "body" : "Entity child" } ),
{ "body" : "Struct child" }
]
} );

expect( user.isLoaded() ).toBeFalse();
expect( user.getPosts() ).toHaveLength( 2 );
expect( user.getPosts()[ 1 ] ).toBeInstanceOf( "Post" );
expect( user.getPosts()[ 1 ].isLoaded() ).toBeFalse();
expect( user.getPosts()[ 1 ].getBody() ).toBe( "Entity child" );
expect( user.getPosts()[ 2 ] ).toBeInstanceOf( "Post" );
expect( user.getPosts()[ 2 ].isLoaded() ).toBeFalse();
expect( user.getPosts()[ 2 ].getBody() ).toBe( "Struct child" );
} );
} );
}

Expand Down
23 changes: 11 additions & 12 deletions tests/specs/integration/GoodErrorMessagesSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,17 @@ component extends="tests.resources.ModuleIntegrationSpec" {
skip = server.keyExists( "boxlang" )
);

it( "throws a helpful error message when trying to set a belongsToMany relationship when the relationship is not loaded", function() {
expect( function() {
getInstance( "Post" ).create( {
"user_id" : 1,
"body" : "A new post body",
"publishedDate" : now(),
"tags" : [ 1, 2 ]
} );
} ).toThrow(
type = "QuickEntityNotLoaded",
regex = "This instance is not loaded so it cannot set the \[tags\] relationship\. Save the new entity first before trying to save related entities\."
);
it( "does not persist a filled belongsToMany relationship when creating the parent", function() {
var post = getInstance( "Post" ).create( {
"user_id" : 1,
"body" : "A new post body",
"publishedDate" : now(),
"tags" : [ 1, 2 ]
} );

expect( post.isLoaded() ).toBeTrue();
expect( post.getTags() ).toBe( [ 1, 2 ] );
expect( post.fresh().getTags() ).toBeEmpty();
} );
} );
}
Expand Down
Loading