Closed marrocmau closed 7 years ago
In this case you might want to use Graph slightly differently. You can use groups in this case, where Boss
is a grouping. This way, you can search for all members that are Bosses, and then check if they have a trip.
let graph = Graph()
graph.clear()
let search = Search<Relationship>(graph: graph).for(types: "Subscriber")
let u1 = Entity(type: "User")
u1["name"]="John"
u1["date"]="08/05/2017"
u1["image"]="img1"
u1.add(to: "Boss")
let u2 = Entity(type: "User")
u2["name"]="Mary"
u2["date"]="08/05/2017"
u2["image"]="img2"
let u3 = Entity(type: "User")
u3["name"]="George"
u3["date"]="08/05/2017"
u3["image"]="img2"
u3.add(to: "Boss")
let trip1 = Entity(type: "Trip")
trip1["location"]="USA"
trip1["vehicle"]="CAR"
u1.is(relationship: "Subscriber").of(object: trip1)
u2.is(relationship: "Subscriber").of(object: trip1)
let trip2 = Entity(type: "Trip")
trip2["location"]="CANADA"
trip2["vehicle"]="BOAT"
u2.is(relationship: "Subscriber").of(object: trip2)
u3.is(relationship: "Subscriber").of(object: trip2)
graph.sync()
let result = search.sync().filter { (subscriber) -> Bool in
return true == subscriber.subject?.member(of: "Boss") && "USA" == subscriber.object?["location"] as? String
}
print(result.subject(types: "User"))
You can also do this:
let graph = Graph()
graph.clear()
let search = Search<Entity>(graph: graph).member(of: "Boss")
let u1 = Entity(type: "User")
u1["name"]="John"
u1["date"]="08/05/2017"
u1["image"]="img1"
u1.add(to: "Boss")
let u2 = Entity(type: "User")
u2["name"]="Mary"
u2["date"]="08/05/2017"
u2["image"]="img2"
let u3 = Entity(type: "User")
u3["name"]="George"
u3["date"]="08/05/2017"
u3["image"]="img2"
u3.add(to: "Boss")
let trip1 = Entity(type: "Trip")
trip1["location"]="USA"
trip1["vehicle"]="CAR"
u1.is(relationship: "Subscriber").of(object: trip1)
u2.is(relationship: "Subscriber").of(object: trip1)
let trip2 = Entity(type: "Trip")
trip2["location"]="CANADA"
trip2["vehicle"]="BOAT"
u2.is(relationship: "Subscriber").of(object: trip2)
u3.is(relationship: "Subscriber").of(object: trip2)
graph.sync()
let users = search.sync().filter { (user) -> Bool in
let subscribers = user.relationship(types: "Subscriber").filter { (subscriber) -> Bool in
return "USA" == subscriber.object?["location"] as? String
}
return 0 < subscribers.count
}
print(users)
If you need any further help, please reopen the issue :) Thank you!
I have this situation and i want search only users (in relationship with Trip) but order by "isBoss" is true