Building Blocks > Extending the Data Model > Creating a Custom Manager
Creating a Custom Manager
The data model uses manager Things to control certain functionality. The manager Thing holds the necessary functions for any database manipulation for the functionality, such as creating and updating database tables. The manager Thing also implements all the Thing Shapes responsible for managing the services related to the associated object model.
These managers Things are registered in the DefaultGlobalManagerConfiguration table on the Configuration page of the PTC.Base.Manager Thing.
You can create custom managers to manage (create and update) your own database tables. Use the manager Things as models.
The following steps describe how to create a manager that manages a database table named customdata, with two columns: UID (primary key, autogenerated) and CustomProperty1 (string data type). The customdata table is initialized with two rows.
1. Create a new building block.
To create a new building block, complete the following steps:
a. Create a new project. Use a unique prefix for your project, such as your company name. The PTC prefix is reserved for entities delivered by PTC. For this example, create a project named MyCompany.MyBuildingBlock. For each new Thing Template, Thing, or other entity that you create for this building block, add your new project as the Project value on the General Information page for the entity.
b. Create a new Thing Template in your project that uses PTC.Base.ComponentEntryPoint_TT as its Base Thing Template. For this example, name the new Thing Template MyCompany.MyBuildingBlock.EntryPoint_TT. If your new building block is extending from a PTC building block, use the entry point Thing Template from the PTC building block as the Base Thing Template for your new building block.
c. Create a new Thing in your project that uses the Thing Template created in step 1.b as its Base Thing Template.
2. In ThingWorx Composer, create a new Data Shape.
a. For Name, enter MyCompany.MyBuildingBlock.CustomData_DS.
b. For Project, select the project for your building block. In this example, MyCompany.MyBuildingBlock.
c. Under Field Definitions, click Add.
d. Define the following field:
Name—Enter UID.
Base Type—Select LONG.
Is Primary Key—Select this checkbox.
e. Click Icon to add the current field and immediately add another field. to add the first field, and define another field:
Name—Enter CustomProperty1.
Base Type—Select STRING.
f. Click Add icon. to add the second field.
g. Click Save to save the new Data Shape.
3. In ThingWorx Composer, create a new Thing Template.
a. For Name, enter MyCompany.MyBuildingBlock.Manager_TT.
b. For Base Thing Template, select the PTC.Base.CommonManager_TT Thing Template. If the new building block is extending from a PTC building block, use the manager Thing Template from the PTC building block as the Base Thing Template.
c. For Implemented Shapes, search for and add PTC.DBConnection.DBManagement_TS. If the manager is used to call create services, search for and add PTC.DBConnection.ServiceConfiguration_TS, as well.
d. Click Save to save the Thing Template.
4. In ThingWorx Composer, create a new Thing.
a. For Name, enter MyCompany.MyBuildingBlock.Manager.
b. For Base Thing Template, search for and select MyCompany.MyBuildingBlock.Manager_TT (the Thing Template created in step 3).
c. Under Services, override the GetDBInfo service and add the following code, then click Done:
var result = {
dbInfo: [{dataShapeName: "CustomData"}]
};
d. Under Services, override the InitData service and add the following code, then click Done:
var infoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "CustomData"
});

infoTable.AddRow({"CustomProperty1":"TestValue1"});
infoTable.AddRow({"CustomProperty1":"TestValue2"});

Things[me.GetDatabaseThingName()].Insert({
infoTable: infoTable,
dataShapeName: "CustomData"
});
e. Click Save to save the Thing.
5. If your new building block is extending from a PTC building block, add any configurations from the manager Thing of the original building block to the manager Thing of the new building block.
a. Navigate to the manager Thing that you created in step 4, in this example MyCompany.MyBuildingBlock.Manager.
b. Under Configuration, add the same configurations that are found on the Configuration page for the manager Thing of the original building block.
6. Register the custom manager.
a. Navigate to the PTC.Base.Manager Thing.
b. Under Configuration, click Add for the DefaultGlobalManagerConfiguration configuration table.
For Name, enter a name for the custom manager, for example, MyCompany.MyBuildingBlock.Manager.
For Value, search for and select the custom manager Thing created in step 4.
c. Click Add to add the custom manager to the configuration table.
d. Click Save to save the updates to the manager Thing.
7. On the MyCompany.MyBuildingBlock.Manager Thing that you created in step 4, under Services, execute the CreateTables service. A database table named customdata is created containing two rows.
8. If you want to define foreign keys, see Adding or Removing Foreign Keys.
9. To change the length of String database columns, see Setting Length for STRING Database Columns.
10. To configure pre- or post-action event dispatching and validation, see Pre-, On-, and Post-Action Event Dispatching and Validation.
Was this helpful?