Welcome Guest, Not a member yet? Register   Sign In
CI4: Handling model errors
#4

(This post was last modified: 08-21-2023, 06:49 PM by kenjis.)

(04-17-2020, 03:06 AM)rmilecki Wrote: As CodeIgniter4 seems to mix two ways of handling errors should my controller always do something like:

PHP Code:
try {
    $post $postModel->insertWithAttachments($data$attachments);
} catch (\
Exception $e) {
    return redirect()->back()->withInput()->with('errors'$e->getMessage());
}

if (!
$post) {
    return redirect()->back()->withInput()->with('errors'$postModel->errors());


This seems a bit complex as for a single model call.

Yes. This is not good design.
But if you use Model, you should write code like that.

Or change all the errors that the Model returns false to an Exception by yourself.
In Shield, we do like that. 
See https://github.com/codeigniter4/shield/b...nTrait.php
Reply


Messages In This Thread
CI4: Handling model errors - by rmilecki - 04-17-2020, 03:06 AM
RE: CI4: Handling model errors - by InsiteFX - 04-18-2020, 03:26 AM
RE: CI4: Handling model errors - by joho - 08-21-2023, 01:27 AM
RE: CI4: Handling model errors - by kenjis - 08-21-2023, 06:42 PM
RE: CI4: Handling model errors - by InsiteFX - 08-21-2023, 08:48 PM
RE: CI4: Handling model errors - by joho - 08-22-2023, 12:01 AM
RE: CI4: Handling model errors - by kenjis - 08-22-2023, 01:06 AM
RE: CI4: Handling model errors - by joho - 08-22-2023, 01:18 AM
RE: CI4: Handling model errors - by kenjis - 08-22-2023, 01:35 AM
RE: CI4: Handling model errors - by joho - 08-22-2023, 02:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB