Search code examples
iosobjective-cdbaccess

SharkORM - How to parse results of Joins


I am trying to use SharkORM to fetch relationships across my objects. I am familiar with the joinTo syntax defined in SharkORM's documentation, but I am not sure how to use the resulting joinedResults object to get the related objects I need.

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

outputs

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

into the Person.joinedResults field, but how do I take those results and get a Department object back. I've tried making a call to person.department after the joinTo, but it seems to make a second query to the database as if I hadn't used joinTo at all.

Am I really expected to parse the dictionary results of person.joinedResults into a Department object manually? That is very cumbersome, especially when you start joining more than one relationship.

I feel as if I am missing some obvious way to use the results of the joinTo.


Solution

  • 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..