问题
I want to be able to filter based on the properties of related models. For example, I have a Class object with a Building object. I want to get a list of classes that take place in a certain building.
This filter
{
"include":"building",
"scope":{
"where":{
"name":"warehouse"
}
}
}
returns all classes, but only includes building if its name is "warehouse". What I want is for the where clause on building name to apply to the whole filter so that I only get the class if it's building has that name.
Is there any way to do what I want?
Thanks!
回答1:
You can do this in code, see include with filters in the docs.
I'm not sure about the JSON but I think it should look more like this:
include: {
relation: 'building',
scope: {
where: {name: 'warehouse'}
}
}
回答2:
At the moment this is not possible. The issue has been described in this topic; https://github.com/strongloop/loopback/issues/517
It looks like Strongloop is not going to implement this feature in the near future.
回答3:
Whenever you want to use it on API CALL , you can follow a model like this one and adapt it to your context.
//Here (as filter) , we get just the most recent message of a chat
{
"include" : {
"relation" : "messages" ,
"scope" :
{
"order" : "createdAt DESC" ,
"limit":1,
"skip":0
}
}
}
来源:https://stackoverflow.com/questions/29321614/loopback-filter-based-on-related-model-properties