Inverted Relation Retrieval¶
Backendless supports a special query syntax for loading a subset of child objects for a specific parent. Consider the following table schemas:
PhoneBook table:
Contact table:
Address table:
These tables will be used to demonstrate how to query Backendless for conditional related object retrieval.
¶
The Map-based approach does not require you defining classes for the objects stored in Backendless. Instead your code can use Map to to store and retrieve objects in/from Backendless.
Consider the following class definitions for the entities from the diagram: PhoneBook class:
Contact class:
Address class:
import 'package:backendless_sdk/backendless_sdk.dart'; import 'contact.dart'; @reflector class PhoneBook { String objectId; Contact owner; List<Contact> contacts; }
import 'package:backendless_sdk/backendless_sdk.dart'; import 'address.dart'; @reflector class Contact { String objectId; String name; int age; String phone; String title; Address address; DateTime updated; }
import 'package:backendless_sdk/backendless_sdk.dart'; @reflector class Address { String street; String city; String state; }
The general structure of a whereClause query to load a collection of child objects for a specific parent object is:
ParentTableName[ relatedColumnName ].parentColumnName COLUMN-VALUE-CONDITION
Both columns relatedColumnName
and parentColumnName
must be declared in a table with name of ParentTableName
. The relatedColumnName
must be a relation column. The table relatedColumnName
points to is the table where the objects must be loaded from. The examples below demonstrate the usage of this syntax:
Find all contacts in a city for a specific phone book¶
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook["objectId"]}' and address.city = 'Smallville'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.of( "Contact" ).find( queryBuilder );
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and address.city = 'Smallville'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );
Find all contacts for the specific phone book where the city name contains letter 'a'¶
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook["objectId"]}' and address.city like '%a%'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.of( "Contact" ).find( queryBuilder );
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and address.city like '%a%'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );
Find all contacts where age is greater than 20 for a specific phone book¶
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook["objectId"]}' and age > 20"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.of( "Contact" ).find( queryBuilder );
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and age > 20"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );
Find all contacts for a specific phone book where age is within the specified range¶
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook["objectId"]}' and age >= 21 and age <= 30"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.of( "Contact" ).find( queryBuilder );
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and age >= 21 and age <= 30"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );
Find all contacts for a specific phone book where age is greater than 20 and the city is Tokyo¶
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook["objectId"]}' and age > 20 and address.city = 'Tokyo'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.of( "Contact" ).find( queryBuilder ); String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and age > 20 and address.city = 'Tokyo'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );
String whereClause = "PhoneBook[contacts].objectId='${savedPhoneBook.objectId}' and age > 20 and address.city = 'Tokyo'"; DataQueryBuilder queryBuilder = DataQueryBuilder() ..whereClause = whereClause; Backendless.data.withClass<Contact>().find( queryBuilder );