danmurphy
06/10/2022, 3:33 PMFoundItem which “hasMany” details. The memento of the FoundItem brings back the details info so this works great…
var foundItem = getInstance( 'FoundItem' ).findOrFail( rc.id ).getMemento();
But how do I filter the details when getting a FoundItem? I only want the details with a status of P (also NULL, but keeping it simple for now). This still brings back all of the details.
var foundItem = getInstance( 'FoundItem' )
.findOrFail( rc.id )
.whereHas( 'details', function( q ) {
q.where( 'status', 'P' );
} )
.getMemento();
I’ve also tried changing the line findOfFail( rc.id ) to firstWhere( 'id', rc.id) with the same results. Thoughts?David Rogers
06/10/2022, 4:05 PMorFail executes the query, and the subsequent whereHas isn't too meaningful. maybe whereID(rc.id).whereHas(...).first()danmurphy
06/10/2022, 4:39 PMdanmurphy
06/10/2022, 5:06 PMdanmurphy
06/10/2022, 5:43 PMDavid Rogers
06/10/2022, 5:50 PMgetInstance("...").retrieveQuery().selectRaw("0 as __someNameToHelpFindItInSixTrillionQueries__").getOrWhatever()danmurphy
06/10/2022, 5:51 PMdanmurphy
06/10/2022, 7:23 PMwil-shiftinsert
06/10/2022, 7:33 PMmappers : {
"filteredDetails" : function( _, memento ) {
return this.details().where("status", "P").asMemento();
}
}
You could also create a relation in your foundItem which ONLY returns items with status P, e.g
function detailsWithP(){
return hasMany("Detail", "some_id").where("status","P")
}
Just make sure your detailsWithP will be included in your memento. Reviewing I think it might even be easier to to it this way, and your filtered details will not only be available in your memento, but in any instance of your FoundItemdanmurphy
06/10/2022, 7:37 PMwil-shiftinsert
06/10/2022, 7:41 PMdanmurphy
06/10/2022, 7:42 PMdanmurphy
06/10/2022, 7:42 PMwil-shiftinsert
06/10/2022, 7:43 PMdanmurphy
06/10/2022, 7:43 PMwil-shiftinsert
06/10/2022, 7:44 PMquick because it is quite different from cborm.danmurphy
06/10/2022, 7:46 PMCOALESCE(fd.status, 'P') = 'P' .wil-shiftinsert
06/10/2022, 7:53 PMfunction detailsWithP(){
return hasMany("Detail", "some_id").where( function( q ) {
q.whereNull( "status")
.orwhere( "status", "P" )
}
}
You can nest any qb function heredanmurphy
06/10/2022, 7:55 PMdanmurphy
06/10/2022, 7:55 PMwil-shiftinsert
06/10/2022, 7:56 PMdanmurphy
06/10/2022, 7:58 PMdanmurphy
06/10/2022, 8:03 PM.whereRaw( "NVL(status, 'P') = 'P'" ) .danmurphy
06/10/2022, 8:05 PM