Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
homestar9 opened this issue Apr 29, 2022 · 3 comments

Comments

@homestar9
Copy link
Contributor

homestar9 commented Apr 29, 2022

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" );

}
@bdw429s
Copy link

bdw429s commented Apr 29, 2022

This issue has been mentioned on Ortus Solutions Community. There might be relevant details there:

https://community.ortussolutions.com/t/quick-4-2-4-this-instance-is-not-loaded-so-it-cannot-access-the-relationship/9216/1

@homestar9
Copy link
Contributor Author

homestar9 commented Apr 29, 2022

One way to enable this behavior would be to make the following code changes:

BaseEntity.cfc

  1. Remove calls to guardAgainstNotLoaded() in relationship calls like in belongsTo()
  2. Add the following to isNullValue()
if ( !arguments.keyExists( "value" ) ) {
     return true;
}

I'm coming from a place of not being very familiar with Quick, but based on some "hack and slash" experiments, this change appears to work.

@homestar9
Copy link
Contributor Author

@elpete Submitted a pull request for your review.
#178

Please be gentle, this is my first Quick PR. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants