quick icon indicating copy to clipboard operation
quick copied to clipboard

Idea/Improvement: Return Null When Requesting Relationship Data from Non Loaded Entities

Open homestar9 opened this issue 2 years ago • 3 comments

Related to this post on the Ortus community forums.

It would be great if, when requesting relationship data from a non-loaded (new) entity, Quick would return null (or an empty array where appropriate) just like if you performed the same operation from a loaded entity. This behavior should be more intuitive and also better follows the null object pattern.

Here's a simple example of how one might use this new change in Quick:

// 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 with the author (it will either be null or populated)
	return prc.post.getMemento( "author" );

}

homestar9 avatar Apr 29 '22 18:04 homestar9