Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting child object from [filter:include] request using LBPersistedModels #63

Open
soltrinox opened this issue Sep 14, 2015 · 0 comments

Comments

@soltrinox
Copy link

I must be missing something wrong because i can not directly translate child objects that are "filter[include]" to their respective model class types. I created a parser method to handle this.
Any recommendations to do it easier or correctly?

I request the child objects through a parent model like this

 [[[self adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/Notes" verb:@"GET"] forMethod:@"Notes.filterOne"];
    [self.notesModelRepository
    invokeStaticMethod:@"filterOne"
    parameters: @{ @"filter[include]":
    @[ @"TAGS", @{@"profile" : @[@"ACCOUNT", @"TYPE", @"MEDIA", @"STATUS" ]}],
    @"filter[where]": @{ @"id" : iid } }
         success:staticMethodSuccessBlock    failure:staticMethodErrorBlock];

My Child Model Class is here

@interface MediaModel : LBPersistedModel
@property (nonatomic, copy) NSString *URL1;         //  (string, optional),
@property (nonatomic, copy) NSDate *URL2;           //  (string, optional),
@property (nonatomic, copy) NSDate *DATA;           //  (any, optional),
@property (nonatomic, copy) NSString *created;            //  (string, optional),
@property (nonatomic, copy) NSString *updated;             //  (string, optional),
@property (nonatomic, copy) NSString *REFERENCE;        //  (any, optional),
@property (nonatomic, copy) NSString *mediaId;          //  (objectid, optional)

//  =====================   RELATIONS  ========================
@property (weak) NSObject *TYPE;
- (MediaModel*) transformMediaModel:(LBPersistedModel *)model;
- (MediaModel*) transformMediaObject:(NSObject *)model;
- (NSMutableArray *) processArrayMediaModels:(NSArray*)model;

@end

And the model looks like this

-(MediaModel*) transformMediaObject:(NSObject *)model{

    [self setValue:[model valueForKeyPath:@"created"] forKeyPath:@"created"];
    [self setValue:[model valueForKeyPath:@"updated"] forKeyPath:@"updated"];
    [self setValue:[model valueForKeyPath:@"id"] forKeyPath:@"mediaId"];
    [self setValue:[model valueForKeyPath:@"TYPE"] forKeyPath:@"TYPE"];

    [self setValue:[model valueForKeyPath:@"URL1"] forKeyPath:@"URL1"];
    [self setValue:[model valueForKeyPath:@"URL2"] forKeyPath:@"URL2"];
    [self setValue:[model valueForKeyPath:@"DATA"] forKeyPath:@"DATA"];
    [self setValue:[model valueForKeyPath:@"REFERENCE"] forKeyPath:@"REFERENCE"];

    return self;
}

- (NSMutableArray *) processArrayMediaModels:(NSArray*)model{
    NSMutableArray *jMEDIA = [[NSMutableArray alloc] init];

    for(NSDictionary *tMEDIA in [model objectAtIndex:0]){
        NSLog(@"MEDIA ITEM: %@", tMEDIA);
        if(tMEDIA == (id)[NSNull null]){
        }else{
            LBRESTAdapter *adapter = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).adapter;
            MediaModelRepository *lbr = (MediaModelRepository*)[adapter repositoryWithModelName:@"Media" persisted:YES ];
            LBPersistedModel *xt  = (LBPersistedModel *)[lbr modelWithDictionary:tMEDIA];
            [jMEDIA addObject:[self transformMediaObject:[xt toDictionary]]];
        }
    }

    return jMEDIA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants