Skip to content

Set Relation using condition

API request replaces related objects with the ones identified in the API call. Child objects are referenced implicitly - through a whereClause defining the condition for object selection.

- (void)setRelationWithColumnName:(NSString * _Nonnull)columnName parentObjectId:(NSString * _Nonnull)parentObjectId whereClause:(NSString * _Nullable)whereClause responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func setRelation(columnName: String, parentObjectId: String, whereClause: String?, responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

- (void)setRelationWithColumnName:(NSString * _Nonnull)columnName parentObjectId:(NSString * _Nonnull)parentObjectId whereClause:(NSString * _Nullable)whereClause responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func setRelation(columnName: String, parentObjectId: String, whereClause: String?, responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
parentObjectId must be objectId of the object which will be assigned related children for columnName.
columnName Name of the column identifying the relation. Objects identified by whereClause will be set as related for the  column in the table containing parentObjectId. The column name may optionally include table name separated by the colon character as well as cardinality which determines the type of relationship (one to one or one to many) (see the note below):

Important

If the column does not exist in the parent table at the time when the API is called, the value of the "columnName" argument must include the name of the child table separated by colon and the cardinality notation. The cardinality is expressed as ":1" for one-to-one relations and ":n" for one-to-many relations. For example, the value of "myOrder:Order:1" will create a one-to-one relation column "myOrder" in the parent table. The column will point to the Order child table. Likewise, the value of "myOrder:Order:n" will create a one-to-many relation column "myOrder" pointing to the Order table.

Argument                Description
whereClause a where clause condition identifying objects in the child table which will be set as the related objects for the parent object.

Return Value

Number of child objects set into the relation.

Example

The following request creates a relation between a Person object and all objects in the Users table which match the provided query. The query is specified in the whereClause argument:

name = 'Joe' OR name = 'Frank' 

As a result of the operation, all User objects where the name property is either Joe or Frank will be set in the relation. The relation column is created if it does not exist. This is done because the column contains the child table qualifier, defined as ":Users:n" right after the column name.

DataStoreFactory *dataStore = [Backendless.shared.data of:[Person class]];
[dataStore setRelationWithColumnName:@"users:Users:n" parentObjectId:@"XXXXX" whereClause:@"name = 'Joe' OR name = 'Frank'" responseHandler:^(NSNumber *relations) {
    NSLog(@"%@ relations have been set", relations);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let dataStore = Backendless.shared.data.of(Person.self)
dataStore.setRelation(columnName: "users:Users:n", parentObjectId: "XXXXX", whereClause: "name = 'Joe' OR name = 'Frank'", responseHandler: { relations in
    print("\(relations) relations have been set")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Person"];
[dataStore setRelationWithColumnName:@"users:Users:n" parentObjectId:@"XXXXX" whereClause:@"name = 'Joe' OR name = 'Frank'" responseHandler:^(NSNumber *relations) {
    NSLog(@"%@ relations have been set", relations);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let dataStore = Backendless.shared.data.ofTable("Person")
dataStore.setRelation(columnName: "users:Users:n", parentObjectId: "XXXXX", whereClause: "name = 'Joe' OR name = 'Frank'", responseHandler: { relations in
    print("\(relations) relations have been set")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})