Reputation: 3472
I have 3 entities, "Bar", "Waiter", "Product" the relationship is as follows, A "Bar" has one to many relationship with "Waiter" and the same from Waiter to Product.
I need to know which Bars has been serving Products with certain attributes.
The entity Im fetching is Bars, So far I've tried:
[NSPredicate predicateWithFormat:@"SUBQUERY(toWaiter, $x, ANY $x.toProduct.dateServed > %@ AND $x.toProduct.dateConsumed > %@).@count > 0), [NSDate date], [NSDate date]];
Where I get
Only allowed one toMany/manyToMany relationship in subquery expression collection
If I try:
[NSPredicate predicateWithFormat:@"SUBQUERY(toWaiter, $x, ANY $x.toProduct.dateServed > %@ ANY AND $x.toProduct.dateConsumed > %@).@count > 0), [NSDate date], [NSDate date]];
Two ANYs, I get the same error,
Only allowed one toMany/manyToMany relationship in subquery expression collection
Which is the correct way to subquery for more than one attribute?
thanks.
This is the model:
Upvotes: 0
Views: 268
Reputation: 21536
You can do this by nesting two SUBQUERY clauses together:
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SUBQUERY(toWaiter, $x, SUBQUERY($x.toProduct, $y, $y.dateServed > %@ AND $y.dateConsumed > %@).@count > 0).@count > 0", [NSDate date], [NSDate date]];
Or, in (vaguely) natural language: "fetch all the Bars
with a non-zero count of (Waiters
with a non-zero count of (Products
with both dateServed
and dateConsumed
greater than today))".
Upvotes: 2