-
Notifications
You must be signed in to change notification settings - Fork 672
Developer Guide: Data Access Objects (DAO's)
All of ThinkTank’s database interactions are encapsulated in DAO’s (Data Access Objects), contained in the model (the common directory).
Let’s draft what all DAO methods should return.
Method return format draft as of now is as follows:
- INSERT Single item: last inserted ID (where there is an AUTO_INCREMENT primary key)
- INSERT Multiple items: number of inserted rows
- UPDATE/DELETE: number of affected rows
- SELECT Multiple rows: array with objects, or arrays if no appropriate class exist, empty array on no rows returned.
- SELECT Single row: object, array if no appropriate class exist, null on no rows returned.
- IF: True if the data exist, otherwise false.
As seen above multiple SELECT formats does exist:
- “SELECT Single row” is for methods that expect only/exactly one result.
- “SELECT Multiple rows” is for methods expecting any number of results. Will not return “SELECT Single row” format upon 1 row returned.
Method naming:
getData* Data Parsing Methods used internally in DAO, do not use this namespace for data accessing.
get* (e.g. getUsersByID) SELECT queries – It is encouraged that you try to use the noun in Singular or Plural according to single or multi-row return.
set* (e.g. setActive) UPDATE Queries
add* (e.g. insertUser) INSERT Queries – It is encouraged that you try to use the noun in Singular or Plural according to single or multi-row insert.
delete* (e.g. deleteUser) DELETE queries
if* (e.g. ifUserActive) IF checks, checks for something, and reports boolean back (true/false)
insert – takes an object as argument, and inserts it to the table.
save – takes an object as an argument, and stores the data in it back to data backend
When you create a class to contain return data, make sure to make the construct so it can handle no-data-input.
Some return methods slipstreams the data directly into the properties, but the construct is still called with no arguments.
For classes that expect arbitrary (no-standard) data, try overloading