richard.herbert
05/22/2023, 8:00 AMrichard.herbert
05/22/2023, 8:01 AMUser
has a Role
relationship and you want to include only the roleName
property, you can do role.roleName
".
I have two objects, an Incident
and a Shop
with the Quick relationship defined in Incident
as...
function shop() {
return belongsTo( 'Shop@v1' );
}
...and the following Incident
memento as...
this.memento = {
defaultIncludes: [
'id'
,'name'
,'status'
,'shop.postcode:postcode'
]
}
... and of course I was expecting a postcode
key in the returned structure but instead I got the whole Shop
memento contained in a shop
key in my structure.
I've tried to read the code but it's beyond my pay grade.
What configuration am I missing or not understanding?sknowlton
05/22/2023, 12:47 PMa.b.c
then it will call a.getShop()
and that's b
and then b.getPostcode()
- I don't think you need :postcode
in there.
As an alternative, you could add a subselect on a
so postcode becomes a property on a and you don't need the intermediary object, which is expensive anyway. Or you could make a mapper that just pulls in that one field, but then you're still paying for the intermediary object instantiationrichard.herbert
05/22/2023, 1:01 PMYou can't skip an intermediary objectAh, okay, so are the docs wrong? I got the impression that was the use case it was describing?
I don't think you needYes, typo in my example, it should have beenin there:postcode
:post_code
you could add a subselect onOkay, I need to look into that.a
Or you could make a mapperYes, I find that later and produced this which seems to work for me
,mappers: {
'post_code': function( item, memento ) { return item.postcode }
}
sknowlton
05/22/2023, 1:02 PMsknowlton
05/22/2023, 1:03 PMsknowlton
05/22/2023, 1:03 PMrichard.herbert
05/22/2023, 1:40 PMelpete
05/22/2023, 2:06 PMignoreDefaults
or something like that to avoid getting the entire Shop object in the output. The default for Quick entities is to include the defined attributes.richard.herbert
05/23/2023, 8:36 AMfunction scopeAddPostCode( qb ) {
qb.addSubSelect( 'post_code', 'shop.postCode' );
}
this.memento = {
defaultIncludes: [
'id'
,'name'
,'status'
,'post_code'
]
}
@elpete I didn't seem to need that ignoreDefaults
, unless I'm missing something?
My next challenge is to get a value I'm calling shopName
which will be a concatenation of the city
and postCode
fields with a ,
between from the related shops
table. I tried...
function scopeAddShopName( qb ) {
qb.addSubSelect( 'shopName', "concat(shop.city, ', ', shop.postcode) AS shopName" );
}
...but no joy - "_Quick couldn't figure out what to do with [concat(shop]._"
I then tried...
function scopeAddShopName( qb ) {
qb.addSubSelect( 'shopName', function( q ) {
q.selectRaw( "concat(shop.city, ', ', shop.postcode) AS shopName" )
.from( "shops" )
.whereColumn( "incidents.shopPK", "<http://shops.pk|shops.pk>" );
} ).from( "incidents" );
}
...but then got "_variable [SUBSELECTQUERY] doesn't exist_".
Not sure what to try next?elpete
05/23/2023, 1:30 PMfrom( "incidents" )
looks like it might be in the wrong place.elpete
05/23/2023, 1:31 PMignoreDefaults
was how to get the entire Shop object but only use certain fields from it.elpete
05/23/2023, 1:31 PMelpete
05/23/2023, 1:32 PMpost_code
and shopCity
you could use mementifier to add a mapper to create shopName
yourself.richard.herbert
05/23/2023, 4:11 PMThat lastThat was an amalgam from Quick docs and the QB docs thinking a SelectRaw might help. I'll looks a mapper, thanks.looks like it might be in the wrong place.from( "incidents" )