Back
beforeDelete()
Registers method(s) that should be called before an object is deleted. This allows you to perform cleanup, enforce constraints, or prevent deletion if certain conditions are not met.
1. Basic usage: run a method before deleting
function config() {
beforeDelete("fixObj");
}
function fixObj() {
// Example: log deletions
writeLog("Deleting record with ID #this.id#");
}
2. Prevent deletion if conditions fail
function config() {
beforeDelete("checkIfAdmin");
}
function checkIfAdmin() {
if (!session.isAdmin) {
throw(type="SecurityException", message="Only admins can delete records.");
}
}
3. Cascade cleanup before deletion
function config() {
beforeDelete("cleanupAssociations");
}
function cleanupAssociations() {
// Delete related comments before removing a post
model("comment").deleteAll(where="postId = #this.id#");
}
Copied!