Set Relation with objects¶
API request replaces existing related objects with the ones identified in the API call. Child objects must be referenced by their objectId
values.
Blocking API¶
int result = Backendless.Data.Of( "TABLE-NAME" ).SetRelation(
Dictionary<string, object> parentObject,
string relationColumnName,
object[] children );
int result = Backendless.Data.Of<E>().SetRelation(
E parentObject,
string relationColumnName,
object[] children );
Non-Blocking API¶
Backendless.Data.Of( "TABLE-NAME" ).SetRelation(
Dictionary<string, object> parentObject,
string relationColumnName,
object[] children,
AsyncCallback<int> callback );
Backendless.Data.Of<E>().SetRelation(
E parentObject,
string relationColumnName,
object[] children,
AsyncCallback;<int> callback );
where:
Argument | Description |
---|---|
TABLE-NAME |
name of the table where the parent object is stored. |
E |
parent's object .NET class. The class name identifies the table where the parent object is stored. |
parentObject |
the object which will be assigned related children for relatedColumnName . When this argument is an instance of System.Collections.Generic.Dictionary (for the dictionary-based approach), it must contain the objectId property. |
relationColumnName |
name of the column identifying the relation. Objects from the children array will be set as related for the column in the parentObject table. 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 "relationColumnName
" 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 |
---|---|
children |
an array of child objects to set into the relation identified by relatedColumnName. For the one-to-one relations the array must contain one element. |
callback |
a responder object which will receive a callback when the relation has been set or if an error occurs. Applies to the non-blocking method only. |
Return Value¶
Number of child objects set into the relation. The non-blocking call receives the return value through a callback executed on the AsyncCallback
object.
Example¶
The example below sets a relation for a one-to-one column named address
declared in the Person
table. The column must be declared in the table prior to the execution of the request shown below. This necessity is explained by missing table name qualifier in the relationColumnName
argument - notice the relation column is "address"
. If the argument value were "address:Address:1"
, then the column would be created automatically.
Dictionary<string, object> parentObject = new Dictionary<string, object>();
parentObject[ "objectId" ] = "41230622-DC4D-204F-FF5A-F893A0324800";
Dictionary<string, object> childObject = new Dictionary<string, object>();
childObject[ "objectId" ] = "3464C734-F5B8-09F1-FFD3-18647D12E700";
object[] children = new object[] { childObject };
AsyncCallback<int> setRelationCallback = new AsyncCallback<int>(
childrenSet =>
{
System.Console.WriteLine( "Number of child objects set in the relation " + childrenSet );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of( "Person" ).SetRelation( parentObject,
"address",
children,
setRelationCallback );
Person personObject = // personObject retrieval is out of scope in this example
Address addressObject = // addressObject retrieval is out of scope in this example
object[] children = new object[] { addressObject };
AsyncCallback<int> setRelationCallback = new AsyncCallback<int>(
childrenSet =>
{
System.Console.WriteLine( "Number of child objects set in the relation " + childrenSet );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of<Person>().SetRelation( personObject,
"address",
children,
setRelationCallback );