Retrieve Errors on Execution of a CakePHP Data Method (e.g. when saving)

Retrieve Errors on Execution of a CakePHP Data Method (e.g. when saving)

Sometimes you need to get the error output when executing a data operation (e.g. saving) in CakePHP.  Use this example to return all SQL errors.

Note: Below, use the $entity (e.g. record, such as $article) and debug, to get all the errors.

Get Errors on Execution of a CakePHP Data Method (e.g. when saving)
Sometimes you need to get the error output when executing a data operation (e.g. saving) in CakePHP.  Use this example to return all SQL errors.

Note: Below, use the $entity (e.g. record, such as $article) and debug, to get all the errors.

CakePHP 4:

if ($this->Model->save($entity)) {   
    //success 
} else {   
    //show MySql errors   
    debug($entity->getErrors()); 
}

 

CakePHP 3:

if ($this->Model->save($entity)) {
  //success
} else {
  //show MySql errors
  debug($entity->errors());
}

Share this Post