1

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

SamGX3
  • 47
  • 1
  • 1
  • 7

1 Answers1

2

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);
}
drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59