[Quick 4.2.4] This Instance Is Not Loaded So It Cannot Access The [] Relationship

As the title suggests, in Quick, if you try to instantiate a new entity and that entity has a relationship, if you try to get a memento of the entity that includes the relationship, you will get an exception.

Here’s a quick example (pun intended):

// Post.cfc
component 
    extends="quick.models.BaseEntity" 
    accessors="true" 
{

    // Post belongs to an Author
    function author() {
        return belongsTo( "User" );
    }

}

// Posts.cfc Handler
function index( event, rc, prc ) {
	
	// Get an existing entity, or return a new one if not found
	prc.post = getInstance( "Post" )
		.firstOrCreate( rc.id );

	// return the memento
	return prc.post.getMemento( "author" );

}

My question is, why throw an error in this scenario? I feel like it would be more intuitive and useful to return null in the author key of the memento just like if you tried to call getAuthor() on a loaded entity that didn’t have an Author attached yet.

Throwing an error is still appropriate if the app attempts to attach an Author to a Post when Post hasn’t been saved yet. However, grabbing the memento, or trying to retrieve the Author from a non-loaded Post smells funny to me and makes it more difficult when developing functionality that may use a loaded or non-loaded entity, like when using the null object pattern.

What do you think?

image

image

I created an issue on Github and a pull request