Skip to content
Closed
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
10 changes: 10 additions & 0 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,16 @@ component accessors="true" {
/**
* Creates a new entity with the given attributes and then saves the entity.
*
* This method always creates and returns a separate entity instance. It does
* not fill, save, or otherwise mutate the entity on which it was called.
* Assign the returned entity when it needs to be used after creation:
*
* ```
* var user = getInstance( "User" ).newEntity();
* user = user.create( { "firstName" : "Dave" } );
* user.isLoaded(); // true
* ```
*
* @attributes A struct of key / value pairs.
* @ignoreNonExistentAttributes If true, does not throw an exception if an
* attribute does not exist. Instead, it skips
Expand Down
15 changes: 15 additions & 0 deletions tests/specs/integration/BaseEntity/CreateSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ component extends="tests.resources.ModuleIntegrationSpec" {
).notToBeNull();
} );

it( "returns a separate loaded entity without mutating the original entity", function() {
var originalUser = getInstance( "User" ).newEntity();
var createdUser = originalUser.create( {
"username" : "Dave",
"first_name" : "Dave",
"last_name" : "Create",
"password" : hash( "password" )
} );

expect( originalUser.isLoaded() ).toBeFalse();
expect( originalUser.isNullAttribute( "username" ) ).toBeTrue();
expect( createdUser.isLoaded() ).toBeTrue();
expect( createdUser.getUsername() ).toBe( "Dave" );
} );

it( "can ignore non-existant properties", function() {
var user = getInstance( "User" ).create(
{
Expand Down
Loading