SamGX3
SamGX3

Reputation: 47

CakePHP 2.9.7 Model::beforeSave()

Is it possible to know what kind(INSERT,UPDATE,DELETE) of query is going to be executed beforeSave() because there is user which can update only and other that can insert only and so on

Upvotes: 1

Views: 61

Answers (1)

drmonkeyninja
drmonkeyninja

Reputation: 8540

To distinguish between an INSERT and an UPDATE you can check if the model's id has been defined:-

public function beforeSave($options = array()) {
    if (! empty($this->id)) {
        // UPDATE
    } else {
        // INSERT
    }

    return parent::beforeSave($options);
}

If content is being deleted then beforeDelete() is called instead of beforeSave().

public function beforeDelete($cascade = true) {
    // DELETE

    return parent::beforeDelete($cascade);
}

Upvotes: 2

Related Questions