gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

why redbean error not working in try catch if there is any error like duplicate key #502

Closed saurabhchandrapatel closed 8 years ago

saurabhchandrapatel commented 8 years ago

why redbean error not working in try catch if there is any error like duplicate key

filisko commented 8 years ago

Hi @saurabhaec, Could you put an example of how you're doing it? It should throw a RedBeanPHP\RedException\SQL exception.

So, if user "saurabhaec" would be already stored in the database and it's unique, the next code would make RedBeanPHP throw an exception and catch it.

try {
    R::exec("INSERT INTO `user` ('saurabhaec')");
} catch (RedBeanPHP\RedException\SQL $e) {
    echo $e->getMessage();
}

But I would recommend you to SELECT and COUNT to check if there is already a value, and then do whatever you want, so in case there is 0 records, INSERT, if there is already 1, show some message to the user.

saurabhchandrapatel commented 8 years ago

try { $autologin = R::dispense('autologin'); $autologin->macid = $macid->id; $autologin->userid = $userid->uid; R::store($autologin);

} catch (Exception $e) { echo $e->getMessage(); }

but its not working.

filisko commented 8 years ago

If there would be a duplicate key, it would throw an exception with the SQL error. Are you sure that it is not being inserted successfully in the database? are you sure that your unique keys are ok? as I told you before, you should check first if there are any duplicates by yourself with a SELECT, and in case there are not any duplicates, insert/update (whatever you want to do) and don't wait for an error to happen.

gabordemooij commented 8 years ago

Closed because of lack of activity.