RestKit:使用外键连接非嵌套关系 - 为源实体提供的无效属性 [英] RestKit: Connecting Non-Nested Relationships Using Foreign Keys - invalid attributes given for source entity
问题描述
我正在尝试从两个单独的 json 文件映射对象(同时使用 RestKit 播种一个 sqlite 数据库).这些文件通过外部 ID file1Code
连接.
结构如下:
文件 1:
<代码>[ {代码":1",活动":假,姓名":乔"},{代码":2",活动":假,姓名":约翰"}]
文件 2:
<预><代码>[{代码":666000,"name": "Hausarzt","file1Code": "1",活动":假}]文件 1 的实体如下所示:
@interface File1Entity : KeyTab
文件 2 的实体如下所示:
@interface File2Entity : KeyTab@property (nonatomic, retain) File1Entitiy *file1Obj;//短暂的@property (nonatomic, retain) NSNumber *file1Code;
KeyTab
(两者都继承)如下所示:
@interface KeyTab : NSManagedObject@property (nonatomic, retain) NSNumber * code;@property (nonatomic, retain) NSString * name;@property (nonatomic, retain) NSNumber * activ;
现在我正在尝试使用 RestKit 文档中的使用外键连接非嵌套关系"此处.
我像这样使用 addConnectionForRelationship
:
[file2EntityMapping addConnectionForRelationship:@"file1Obj"connectedBy:@{@"file1Code": @"code"}];
但是收到错误消息无法连接关系:为源实体提供的属性无效",因为 file1Obj
是一个属性而不是一个属性.
这是在 RestKit 中执行此操作的正确方法吗?
您需要在 File2Entity
上添加 file1Code
作为瞬态属性并在映射期间设置它.映射完成后外键连接完成,原来的JSON不再可用(因此出现数据不存在的错误).
I am trying to map objects from two separate json files (while seeding an sqlite db with RestKit). The files are connected by a foreign id file1Code
.
The structure looks like this:
File 1:
[ {
"code": "1",
"activ": false,
"name": "Joe"
},
{
"code": "2",
"activ": false,
"name": "John"
}
]
File 2:
[
{
"code": 666000,
"name": "Hausarzt",
"file1Code": "1",
"activ": false
}
]
Entity for File 1 looks like this:
@interface File1Entity : KeyTab
Entity for File 2 looks like this:
@interface File2Entity : KeyTab
@property (nonatomic, retain) File1Entitiy *file1Obj;
// Transient
@property (nonatomic, retain) NSNumber *file1Code;
KeyTab
(from which both inherit) looks like this:
@interface KeyTab : NSManagedObject
@property (nonatomic, retain) NSNumber * code;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * activ;
Now I am trying to use the "Connecting Non-Nested Relationships Using Foreign Keys" from the RestKit documentation found here.
I am using addConnectionForRelationship
like this:
[file2EntityMapping addConnectionForRelationship:@"file1Obj"
connectedBy:@{@"file1Code": @"code"}];
But get the error message "Cannot connect relationship: invalid attributes given for source entity" since file1Obj
is a property and not an attribute.
Is this the right way to do this in RestKit?
You need to add file1Code
as a transient attribute on File2Entity
and set it during the mapping. The foreign key connection is completed after the mapping so the original JSON is no longer available (hence the error that the data doesn't exist).
这篇关于RestKit:使用外键连接非嵌套关系 - 为源实体提供的无效属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!