Skip to content

Class to Table Mapping

When you use custom classes in your application for data persistence, Backendless maps the database table names to the names of the classes. For example, the following code is automatically mapped to the Person table:

@interface Person : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber *age;

@end
@objcMembers class Person: NSObject {
    var name: String?
    var age: NSNumber?
}

The Person table in the Backendless database:

person-table

When the name of the class is different than the name of the corresponding table in the database, it is necessary to customize the mapping. This can be achieved with the following API:

[[Backendless.shared.data of:[YOUR-CLASS class]] mapToTableWithTableName:@"TABLE-NAME"];
Backendless.shared.data.of(YOUR-CLASS.self).mapToTable(tableName: "TABLE-NAME")

where:

Argument                Description
"TABLE-NAME"  name of the table to map to a class.
YOUR-CLASS reference to a class to map to the table.

The API is not necessary if the class name is the same as the table name. The only exception to this rule is when your application retrieves related objects using the "Custom Class" approach. In this case, if application should establish mappings between client-side classes and the related (children) tables. For example, consider the following schema:

Order (parent) table:

parent-order-table

OrderItem (child) table:

child-orderitem-table

If any related OrderItem objects are retrieved along the parent Order object (via auto-load or single-step relation retrieval), then the application must establish a mapping for the OrderItem table before the data is retrieved.

[[Backendless.shared.data of:[OrderItem class]] mapToTableWithTableName:@"OrderItem"];
Backendless.shared.data.of(OrderItem.self).mapToTable(tableName: "OrderItem")