CakePHP 3 - Catch Error

use Cake\Core\Exception\Exception;


for($i=1; $i<count($values); $i++) {
        $entity = $table->newEntity();

        // irrelevant code

        try {
            $table->save($entity);
        } catch (Exception $e) {
            $errors[$i-1] = $values[$i];
        } finally {
            if(count($errors) == 0)
                $this->Flash->success('All rows are successfully imported. ');
            else {
                $this->Flash->error('Not all rows are successfully imported. ');
                debug($errors);
            }
        }
    }

What I want to do is to catch conflicting objects and show them to the user.

I get a PDO exception. Those that do not conflict are still inserted what I want.

So I just want to catch the PDO exception, but how?

+4
source share
1 answer

If you only want to catch a specific exception, specify the exception class in the catch block.

try
{}
catch (\PDOException $e)
{}
+3
source

Source: https://habr.com/ru/post/1628086/


All Articles