Skip to content

Commit

Permalink
fix(HasOneOrMany): Allow returning a new non-persisted entity
Browse files Browse the repository at this point in the history
Being able to create a new `HasOneOrMany` relationship entity is great, but sometimes you need access to the new entity before persistence.  Adding a `newEntity()` method gives additional flexibility and also plays nicely with other relationship types.
  • Loading branch information
homestar9 authored May 31, 2024
1 parent 9709b2f commit 084a089
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions models/Relationships/HasOneOrMany.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,20 @@ component
* @return quick.models.BaseEntity
*/
public any function create( struct attributes = {} ) {
var newInstance = variables.related.newEntity().fill( arguments.attributes );
return newEntity()
.fill( arguments.attributes )
.save();
}

/**
* Returns a new instance of the entity, pre-associated to the parent entity. Does not persist it.
*
* @return quick.models.BaseEntity
*/
public any function newEntity() {
var newInstance = variables.related.newEntity();
setForeignAttributesForCreate( newInstance );
return newInstance.save();
return newInstance;
}

/**
Expand Down

0 comments on commit 084a089

Please sign in to comment.