lawrenceagbani
lawrenceagbani

Reputation: 147

Parse: Calling created data relations in android

I have two classes on Parse.com named Companies and Offers and the Offers class has a pointer column to Companies, so for every company there can be multiple offers. How can I create a query in android(Java) that will display all offers for a company based on the relationship?

Edit: Other details I didn't add:- There's actually four classes and they are: Offers, Companies, Venues and Cities. I need to get and display "offers" from all "venues" of a "company" in a "city" or cities.

Upvotes: 2

Views: 47

Answers (1)

kRiZ
kRiZ

Reputation: 2316

ParseQuery<ParseObject> query = ParseQuery.getQuery("Offers");
query.whereEqualTo("yourCompanyPointerColumnName", yourCompanyParseObject);
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> offersList, ParseException e) {
        if (e == null) {
            // Offers retrieved successfully
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});

yourCompanyPointerColumnName is the name of the column in your Offers class which is a pointer to Companies class.

yourCompanyParseObject is the Company object for which you want to retrieve Offers for.

UPDATE:

To get all Offers for all the Companies, just remove the whereEqualTo and this time use the include():

ParseQuery<ParseObject> query = ParseQuery.getQuery("Offers");
query.include("yourCompanyPointerColumnName");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> offersList, ParseException e) {
        if (e == null) {
            // All Offers retrieved successfully
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});

Upvotes: 1

Related Questions