nicklockwood / FXForms

[DEPRECATED]
Other
2.93k stars 339 forks source link

Coredata relation option picker #78

Open tleafs opened 10 years ago

tleafs commented 10 years ago

Not sure if this is there already, but can't seem to get it to work.

How to display and set another MO entity from a relationship.

For example on the Client Form which points to SalesAgent Entity. How to pick the Sales Agent (which has name, id, etc) display just name and then assign the selected SalesAgent MO to the Client form?

bartvandendriessche commented 10 years ago

You can do this by implemenig - (NSString *)fieldDescription on your SalesAgent entity.

If SalesAgent is a OneToMany relation and already conforms to the FXForm protocol, you should define the field as something like this in your Client+Form:

- (NSArray *)fields {
    return @[
             @{
                 FXFormFieldKey: @"salesAgent",
                 FXFormFieldOptions: [self salesAgents],
                 }];
}

- (NSArray *)salesAgents {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SalesAgent"];

    return [[CoreDataStack defaultStack].managedObjectContext executeFetchRequest:request error:nil];
}
tleafs commented 10 years ago

Brilliant! The - (NSString *)fieldDescription took care of the issue.