我在使用 RestKit 建立一些关系时遇到了最困难的时期。
从item
到venue
之间存在一对多关系,但通过多个级别的关系。
这是 JSON 结构的概要:
{
"id": 1,
"name": "Venue Name",
"sections": [
{
"id": 1,
"name": "Paper",
"categories": [
{
"id": 1,
"name": "Paper Goods",
"items": [
{
"venue" : 1
// Other data goes here
}
}
}
}
我正在尝试将 venue
外键连接到 Venue
的 id
属性。
我已尝试执行以下操作:
NSEntityDescription * itemEntity = [NSEntityDescription entityForName:@"DSItem" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
NSRelationshipDescription * venueRel = [itemEntity relationshipsByName][@"venue"];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:venueRel attributes:@{@"venue" : @"pk"}]; // id --> pk on DSVenue class
但对象映射后 field 属性为nil。
如何在其中使用@parent
来创建外键关系?或者有更好的方法吗?
类似于:
@{@"@parent.@parent.@parent.pk" : @"venue"}
我看过this这是一个问题,但它似乎适用于将属性链接到父属性,而不是将外键链接到实体。
任何帮助将不胜感激!
编辑:这是所有的映射代码
RKEntityMapping *itemMapping = [RKEntityMapping mappingForEntityForName:@"DSItem" inManagedObjectStore:managedObjectStore];
itemMapping.identificationAttributes = [DSItem identificationAttributes];
[itemMapping addAttributeMappingsFromDictionary:[DSItem attributeMappings]];
NSEntityDescription * itemEntity = [NSEntityDescription entityForName:@"DSItem" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
NSRelationshipDescription * venueRel = [itemEntity relationshipsByName][@"venue"];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:venueRel keyPath:@"venue"];
[itemMapping addConnection:connection];
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"DSCategory" inManagedObjectStore:managedObjectStore];
categoryMapping.identificationAttributes = [DSCategory identificationAttributes];
[categoryMapping addAttributeMappingsFromDictionary:[DSCategory attributeMappings]];
[categoryMapping addRelationshipMappingWithSourceKeyPath:@"items" mapping:itemMapping];
RKEntityMapping *sectionMapping = [RKEntityMapping mappingForEntityForName:@"DSSection" inManagedObjectStore:managedObjectStore];
sectionMapping.identificationAttributes = [DSSection identificationAttributes];
[sectionMapping addAttributeMappingsFromDictionary:[DSSection attributeMappings]];
[sectionMapping addRelationshipMappingWithSourceKeyPath:@"categories" mapping:categoryMapping];
RKEntityMapping *venueMapping = [RKEntityMapping mappingForEntityForName:@"DSVenue" inManagedObjectStore:managedObjectStore];
[venueMapping addAttributeMappingsFromDictionary:[DSVenue attributeMappings]];
venueMapping.identificationAttributes = [DSVenue identificationAttributes];
[venueMapping addRelationshipMappingWithSourceKeyPath:@"sections" mapping:sectionMapping];
RKResponseDescriptor *venueDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:[DSVenue pathPattern]
keyPath:[DSVenue keyPath]
statusCodes:successIndexSet];
RKResponseDescriptor *itemDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemMapping
method:RKRequestMethodGET
pathPattern:[DSItem pathPattern]
keyPath:[DSItem keyPath]
statusCodes:successIndexSet];
[manager addResponseDescriptorsFromArray:@[itemDescriptor, venueDescriptor]];
最佳答案
只是不要做你想做的事。您的映射看起来很好,假设您在模型中配置了反向关系,您只需从项目实体导航关系即可到达关联的 field 实体。
也就是说,我认为 @parent.id
引用应该可以使连接成为可能(通过授予对 id 的访问权限)。但话又说回来,确实不需要。您可以获取模型并对其进行预测,以获取您所拥有的关系所需的任何信息。
关于ios - Restkit 在嵌套外键关系中使用@parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213500/