From 6bd8051a70d32fe73c1965cb449d2f40d403260a Mon Sep 17 00:00:00 2001 From: Jon Olson Date: Tue, 13 Oct 2009 03:21:14 +0800 Subject: [PATCH] Update mogenerator to handle LHS keyPaths for relationships in predicates --- mogenerator.m | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/mogenerator.m b/mogenerator.m index d7de585c..c6e39362 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -68,6 +68,22 @@ - (NSDictionary*)fetchRequestTemplates { } return result; } + +- (NSString *)_resolveKeyPathType:(NSString *)keyPath { + NSArray *components = [keyPath componentsSeparatedByString:@"."]; + + // Hope the set of keys in the key path consists of solely relationships. Abort otherwise + + NSEntityDescription *entity = self; + for (NSString *key in components) { + NSRelationshipDescription *relationship = [[entity relationshipsByName] objectForKey:key]; + assert(relationship); + entity = [relationship destinationEntity]; + } + + return [entity managedObjectClassName]; +} + - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_ { if (!predicate_) return; @@ -95,10 +111,7 @@ - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bin if (attribute) { type = [attribute objectAttributeType]; } else { - // Probably a relationship - NSRelationshipDescription *relationship = [[self relationshipsByName] objectForKey:[lhs keyPath]]; - assert(relationship); - type = [[relationship destinationEntity] managedObjectClassName]; + type = [self _resolveKeyPathType:[lhs keyPath]]; } type = [type stringByAppendingString:@"*"];