Back

afterCreate()

Registers one or more callback methods that are automatically executed after a new object is created (i.e., after calling create() on a model). This is part of the model lifecycle callbacks in Wheels.

Name Type Required Default Description
methods string No Method name or list of method names that should be called when this callback event occurs in an object's life cycle (can also be called with the method argument).
1. Single callback method
// Instruct Wheels to call the `fixObj` method after an object is created
afterCreate("fixObj");

function fixObj() {
    variables.fixed = true;
}

2. Multiple callbacks
afterCreate("logCreation,notifyAdmin");

function logCreation() {
    writeLog("New record created at #now()#");
}

function notifyAdmin() {
    // send an email notification
}

3. With object attributes
afterCreate("setDefaults");

function setDefaults() {
    if (!len(variables.status)) {
        variables.status = "pending";
    }
}

4. Practical usage in User.cfc
component extends="Model" {
    function config() {
        afterCreate("assignRole,sendWelcomeEmail");
    }

    function assignRole() {
        if (isNull(roleId)) {
            roleId = Role.findOneByName("User").id;
        }
    }

    function sendWelcomeEmail() {
        // code to send welcome email
    }
}
Copied!