Model Class
The Model
class provides an object that can represent a single row of data from a configured database table. The Service Class works with the Model
class when it queries, updates, and deletes records from configured tables.
Event Hooks
Event hooks are pre-existing methods that are defined on the Model class and are called automatically during the SaveChanges() and DeleteRecord() methods.
These event hooks in the base class are left un-implemented so that they can be overridden in a child class in order to extend functionality.
Below is a list of the event hooks with a short description of when they are invoked and suggestions on how they should be used.
List of Event Hooks
_DoBeforeSave
Called before saving any changes (both insert and update).
_DoAfterSave
Called after any changes have been saved (both insert and update).
_DoBeforeInsert
Called before inserting a new record.
_DoAfterInsert
Called after inserting a new record.
_DoBeforeUpdate
Called before updating an existing record.
_DoAfterUpdate
Called after updating an existing record.
_DoBeforeDelete
Called before deleting a record.
_DoAfterDelete
Called after deleting a record.
Methods
SaveChanges
Description
Saves the data for the model into the database by either inserting a new record or updating an existing record.
Will automatically call relevant Event Hooks.
Syntax
SaveChanges(txID = None)
- Parameters
- string txID - (Optional) The id of a transaction to be used for inserting/updating the database. Defaults to
None
.
- string txID - (Optional) The id of a transaction to be used for inserting/updating the database. Defaults to
Examples
tableName = 'myTable'
schema = 'myDB'
service = exchange.pikaview.service_factory.GetModelServiceClass(tableName, schema)
record = service.querySingleById(id = 1)
record.magicnumber = 321
record.SaveChanges()
DeleteRecord
Description
Deletes a record from the database.
Will automatically call relevant Event Hooks.
Syntax
DeleteRecord(txID = None)
- Parameters
- string txID - (Optional) The id of a transaction to be used for inserting/updating the database. Defaults to
None
.
- string txID - (Optional) The id of a transaction to be used for inserting/updating the database. Defaults to
Examples
tableName = 'myTable'
schema = 'myDB'
service = exchange.pikaview.service_factory.GetModelServiceClass(tableName, schema)
record = service.querySingleById(id = 1)
record.DeleteRecord()