Order of operations for lifecycle events
Lifecycle events are an important feature when we want to introduce project-specific behaviors/constraints that extend beyond a database engine’s scope. Knowing the lifecycle events is only half of the equation, and the other half consists of knowing when those lifecycle events are triggered.
Suppose we were given the task to offer all of our products for free to employees. The first action could be adding a beforeValidate
hook that would set the transaction’s subtotal to 0
if the user was an employee. That’s easy for us, but unfortunately a nightmare for the accounting department. A better approach would be to add an additional item that represents the employee discount, using the beforeValidate
or beforeCreate
hook.
The real answer in knowing which lifecycle events to use depends on the project’s requirements. From our previous example, some transactions require moving legal tender, which involves charging...