Cake-Framework [SOLVED]: CakePHP 2.9.7 Model::beforeSave()

Cake-Framework [SOLVED]: CakePHP 2.9.7 Model::beforeSave()

Home Forums Frameworks CakePhp Cake-Framework [SOLVED]: CakePHP 2.9.7 Model::beforeSave()

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #118392

    Cloudy Point
    Keymaster

    QuestionQuestion

    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

    #118393

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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);
    }
    

    Source: https://stackoverflow.com/questions/44194686/cakephp-2-9-7-modelbeforesave
    Author: drmonkeyninja
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.