How to check if an INSERT or UPDATE was in the CakePHP save method?

1

Hello, I have a script that I get an ID for a POST notification along with other data. It turns out that the ID is from a transaction created on another system (IDs are not generated by auto_increment).

An example data I get would be:

{'id' : 1234, 'data' : '2014-06-02', 'etc' : 'teste'}

I create the array based on this json that I get by POST.

$dados['Model'] = array(
   'id' => $json->id, 
   'data' => $json->data, 
   'etc' => $json->etc
);
$this->Model->save($dados);

How to tell if the save () method in the controller made an update or an insert since the id is my primary key?

    
asked by anonymous 02.06.2014 / 22:39

1 answer

1

As I needed in the controller I ended up putting the afterSave () callback in the model as follows.

class Model extends AppModel {

    public $sqlType = false;

    public function afterSave($created){
        if ($created){
            $this->sqlType = 'INSERT';
        } else {
            $this->sqlType = 'UPDATE';
        }
    }
}

and no controller

$this->Model->save($dados);
if ($this->Model->sqlType == 'INSERT'){

}
    
02.06.2014 / 23:27