[Quick 4.2.4] How to Add Subselect for Relationship Count?

I’d like to add a subselect to a Gallery entity to display the count Media entities it contains via a hasMany() relationship. However, I keep running into the This instance is not loaded, so it cannot access the [] relationship blocker.

Here’s how I have the relationship and scope set up:

/**
 * HasMany: Gallery hasMany Media
 */
function media() {
    return hasMany( "Media@cms", "mediaId", "id" ); 
}

/**
 * Scope: With Media Count
 */
function scopeWithMediaCount( query ) {
    addSubselect( "mediaCount", media().count() );
}

Is there a better way to accomplish this? Or do I have to define the inverse belongsTo() relationship in the Media entity?

1 Like

Thanks, @elpete. I was looking at the wrong section in the docs.