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

[Quick 7.2.0] Retrieving Entity through Relationship with FirstOrNew() Returns Relationship Class Instead of Entity Class #243

Open
homestar9 opened this issue Dec 18, 2023 · 0 comments
Milestone

Comments

@homestar9
Copy link
Contributor

homestar9 commented Dec 18, 2023

In the docs, it states you can retrieve entities through a relationship definition. However, it only seems to work with some entity getters like first(), get(), etc. If you try to get an entity using something like firstOrNew(), it will return an instance of the relationship itself instead of the entity class you expected.

firstOrNew() is a very important getter because it allows you to implement the null object pattern so you can call isLoaded() on the resulting response to see if a match was found or not.

Here's a sample relationship definition to trigger the issue:

// Category entity

// category has many posts
function posts() {
        return hasMany( "Post", "postId" );
}

Now try to retrieve an array of Post entities in your handler. The first way works, but the second does not!

// works!
prc.category.posts().where( "id", "=", 1 ).first(); // Returns instance of 'Post`

// does not work!
prc.category.posts().where( "id", "=", 1 ).firstOrNew(); // Returns instance of 'quick.models.Relationships.HasMany'
@elpete elpete added this to the v9.0.0 milestone May 31, 2024
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