quick
quick copied to clipboard
[Quick 7.2.0] Retrieving Entity through Relationship with FirstOrNew() Returns Relationship Class Instead of Entity Class
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'