• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fatal Errors when DataBase command fails

#1
Hello all, I'm having a struggle with Fatal Errors being thrown when some of my Databases commands don't return successfully, for example:

Code:
$flag = true;
$db = \Config\Database::connect();
$builder_audio = $db->table('audios');

$data = [
'name' => $name,
'path' => $path
];

$builder_audio->insert($data);

if(!$db->affectedRows()){
$flag = false;
return $flag;
}

return $flag;

In my Database (Postgres) I created an UNIQUE KEY for the column "name". So when I try to insert a name that already exists I would like to get a return of none "affectedRows", and then deal with this return in my function, but when the error occurs, I get a Fatal Error, my request returns instantly with code 500 and an error description that follows:

Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Type is not supported". in /var/www/html/mysite/system/Format/Exceptions/FormatException.php:9

I could create an SELECT to search for the name before the INSERT, but I'm planning in creating some control in my database using UNIQUE KEYS for a better setup.

Thanks in advance
Reply

#2
replace "insert" with "save"
$builder_audio->save($data);
Reply

#3
(10-18-2019, 10:14 AM)titounnes Wrote: replace "insert" with "save"
$builder_audio->save($data);

Got this error (method "save" does not exist):
    "title": "Error",
    "type": "Error",
    "code": 500,
    "message": "Call to undefined method CodeIgniter\\Database\\Postgre\\Builder::save()",
    "file": "/var/www/html/mysite/app/Models/AudiosModel.php",
    "line": 33,
    "trace": [

Remembering: I'm using CodeIgniter 4
Reply

#4
Instead of checking for the number of affected rows, catch the Exception that's being thrown. You can handle the same situation you're trying now within the catch block.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
(10-18-2019, 01:41 PM)kilishan Wrote: Instead of checking for the number of affected rows, catch the Exception that's being thrown. You can handle the same situation you're trying now within the catch block.

I was already using an Try Catch when I call this function (it is inside a Modal). But I tried to make the Try Catch just around the Insert method or all the my $db function but it didn't prevent the request from being shut down and return status 500 to my browser.
I tried catch(Exception $ex){... and catch(FormatException $ex){

Here is what is happening to the request: (repair the echo "Hello World 1!!\n" and echo "Hello world 2!!\n")
http://prntscr.com/pl9oub

And here is the code I'm running:

Code:
public function addAudio($name, $path){

$db = \Config\Database::connect();
$builder_audio = $db->table('audios');
$flag = true;
try{

$db->transStart();

$data = [
'name' => $name,
'path' => $path
];
echo "Hello world 1!!\n";
$builder_audio->insert($data);
echo "Hello world 2!!\n";
if(!$db->affectedRows()){
$flag = false;
return $flag;
}

$db->transComplete();
return $flag;
}catch(FormatException $ex){
var_dump($ex);
die;
}

}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.