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

Connecting Non-Nested Relationships Using Foreign Keys #26

Closed
Beniamiiin opened this issue Apr 10, 2015 · 4 comments
Closed

Connecting Non-Nested Relationships Using Foreign Keys #26

Beniamiiin opened this issue Apr 10, 2015 · 4 comments

Comments

@Beniamiiin
Copy link

Hello.

FastEasyMapping support non-nested relationships using foreign keys like RestKit?

Connecting Non-Nested Relationships Using Foreign Keys

JSON attributes are sometimes foreign key identifiers to existing Core Data entities, such as entities obtained by a separate RKObjectRequestOperation. RestKit provides a method to map to an entity
using a foreign key identifier. Consider the following JSON:

{ "cities": [
      { "cityID": 1, "cityName": "Chicago" },
      { "cityID": 2, "cityName": "New York" }],
  "users": [
      { "userID":101, "userName": "Jeff", "cityID": 1 },
      { "userID":102, "userName": "Mary", "cityID": 2 },
      { "userID":103, "userName": "Sam",  "cityID": 1 }]
}

and the corresponding models where User has a Core Data relationship to City:

@interface City: NSManagedObject
    @property (nonatomic, retain) NSNumber* cityID;
    @property (nonatomic, retain) NSString* cityName;
@end

@interface User: NSManagedObject
    @property (nonatomic, retain) NSNumber* userID;
    @property (nonatomic, retain) NSString* userName;
    @property (nonatomic, retain) NSNumber* cityID;
    @property (nonatomic, retain) City* city;
@end

To connect the User entity to the City entity referenced by cityID property, you do the following after creating the RKEntityMapping for the User:

[userMapping addConnectionForRelationship:@"city" connectedBy:@{@"cityID": @"cityID"}];

The relationship value @"city" specifies the relationship object or name of the relationship object that is to be connected. The connectedBy: parameter is an NSDictionary mapping the User attribute to the City attribute. Since the attribute names are identical, the connectedBy: parameter can be also specified by an NSString, i.e. @"cityID".

@dimazen
Copy link
Contributor

dimazen commented Apr 14, 2015

Hello, @BenjaminSarkisyan
In short: yes, it is possible.
All you need to do is: add relationship to the entity mapping. In entity mapping you should have only one Attribute Mapping for PK property and no key path.

@Beniamiiin
Copy link
Author

@dimazen Thank you for your reply, but can you write example please.

@dimazen
Copy link
Contributor

dimazen commented Apr 14, 2015

Sure:

FEMManagedObjectMapping *userMapping = [FEMManagedObjectMapping mappingForEntityName:@"User"];
[userMapping setPrimaryKey:@"userID"];
[userMapping addAttributeWithProperty:@"userID" keyPath:@"userID"];

FEMManagedObjectMapping *cityMapping = [FEMManagedObjectMapping mappingForEntityName:@"City"];
[cityMapping setPrimaryKey:@"cityID"];
[cityMapping addAttributeWithProperty:@"cityID" keyPath:nil];

[userMapping addRelationshipMapping:cityMapping forProperty:@"city" keyPath:@"cityID"];

@Beniamiiin
Copy link
Author

Thank you very much.

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