SharkORM - 如何解析连接的结果

时间:2017-07-10 14:50:53

标签: ios objective-c dbaccess

我正在尝试使用SharkORM来获取我的对象之间的关系。我熟悉SharkORM文档中定义的joinTo syntax,但我不知道如何使用生成的joinedResults对象来获取我需要的相关对象。

[[Person query] joinTo:[Department class] leftParameter:@"department" targetParameter:@"Id"]

输出

{
    "Department.Id" = 61;
    "Department.location" = 35;
    "Department.name" = Development;
}

进入Person.joinedResults字段,但是如何获取这些结果并获取一个Department对象。我试过在joinTo之后调用person.department,但它似乎对数据库进行了第二次查询,好像我根本没有使用过joinTo。

我真的希望手动将person.joinedResults的字典结果解析为Department对象吗?这非常麻烦,特别是当你开始加入多个关系时。

我觉得好像错过了一些使用joinTo结果的明显方法。

1 个答案:

答案 0 :(得分:1)

The join functionality is in addition to relationships. So you can reference unrelated (or related) tables in a query, but then objects you get back can only ever be physically structured as per your original class, and are defined from the data and not from your query.

So, in a nut shell; joinTo: will enable you to reference remote or unrelated objects in your query for selection.

But to traverse object relationships you would use the properties.

Person* p = GetFirstPerson()
// reference/traverse the object to look at relationships (1:1)
p.department

or

p.department.location

So I guess what i'm saying is, even though it is SQL like syntax, you can only ever end up with rigidly defined classes in a fixed structure as a result unless using other interfaces such as sum, distinct etc..