Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two tables using parse

I have one database at parse.com

Where I have one class called UserDetail

In that class I have one array Field called occupationid

For this column i have another class Called Occupation where columns are like occupationid, occupationname

Now I want to fetch all data from UserDetail as well as all data from occupation class according to the occupationid stored in UserDetail


UserDetail

Username - occupationid - Detail

Jhon - ["1","3","4"] - ajfdkaldjlajsdkfjaldkf


Occupation

occupationid - occupation

1 - accountant

2 - lawyer

3 - painter

4 - writer

like wise.

I have gone through the document and about PFRelation i read but didn't get how to apply that in my case to fetch this kind of data.

Also i have tried with

    PFQuery *getuserdetail = [PFQuery queryWithClassName:@"UserDetail"];

    [getuserdetail orderByAscending:@"celebid"];

    PFQuery *getoccupation = [PFQuery queryWithClassName:@"Occupation"];
    [getoccupation whereKey:@"occupationid" matchesKey:@"occupationid" inQuery:getuserdetail];

Any suggestions? I have to do the same for other 2,3 columns. Any help will be appreciated.

UPDATE

Data is adding at the parse.com DataBrowser directly and not from the code.

I created one column with type Relation in UserDetail class Which ask me to add occupationid and occupation

When i added that i face two problems

1> When I try to add the same occupationid for different user it is creating two rows for the same occupationid. It means for two user having same occupation it is creating two rows in occupationtable eg. objectid - occupationid - occupation adfk12 - 1 - School teacher jdlkfg56 - 1 - School teacher

2> When I tried to fetch from the code it is giving me errors

PFQuery *getuserdetail = [PFQuery queryWithClassName:@"UserDetail"];        
[getuserdetail orderByAscending:@"celebid"];
[getuserdetail includeKey:@"occupation"];
[getuserdetail whereKey:@"userid" equalTo:[NSString stringWithFormat:@"%d",intuserID]];

occupationid = "(<00000000>.(null) -> Occupation)";

where Occupation is the class name occupation is the column name. I tried with editing column name occupation with occupationname if it is getting ambiguity. But that also not working.

like image 985
Heena Avatar asked Jun 21 '13 05:06

Heena


1 Answers

I think you're getting a bit unstuck because you're thinking of Parse in terms of a 'normal' SQL-like database. I say this because from what you've written, it looks as if your occupationid field is redundant (because Parse automatically assigns all objects a unique ID). You can use PFRelation to associate an occupation with a user, like so:

[someUser setObject:someOccupation forKey:@"occupation"]

This will set a pointer on someUser, under the key occupation.

However, as you might have found out (and maybe why you're not sure how to apply PFRelation properly) is that by default Parse doesn't fetch related objects when querying. You need to explicitly tell Parse to do this.

To do this, you use the includeKey: method. So, to query for a user and get their occupation at the same time (assuming you've set the occupation as a PFRelation), you can do this:

PFQuery *query = [PFQuery queryWithClassName:@"UserDetail"];
[query includeKey:@"occupation"];
[query findObjectsInBackground... etc 

My best advice would be to try not to think of Parse like MySQL or similar, since it works in a rather different way. I find Parse's documentation to be really useful and clear, so perhaps it might be worth looking there also.

like image 182
lxt Avatar answered Sep 20 '22 22:09

lxt