Search code examples
ormfat-free-framework

How do I check the result of a Fat Free Mapper call to save a DB record?


I'm using the SQL Mapper to update records in my MYSQL DB which is working ok - but I can't see how to check what the outcome of the update was from the call to save() or update().

Update seems to return the entire mapper object which doesn't offer an obvious way of checking if the update failed. Should I be checking the return code or catching an exception?

Any help appreciated!

Matt


Solution

  • libregeek is right, you should enable PDO exceptions and catch them:

    $db=new \DB\SQL($dsn,$user,$pwd,[
      \PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION]
    );
    
    $mytable=new \DB\SQL\Mapper($db,'mytable');
    
    try {
    
      $mytable->copyfrom($input);
      $mytable->save();
      echo 'Data successfully saved';
    
    } catch(\PDOException $e) {
    
      echo 'Something went wrong';
      // let's find what exactly:
      $err=$e->errorInfo;
      echo $err[0];// PDO error code
      echo $err[2];// driver specific error message
    
    }
    

    See also this topic.