DAO pattern help

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
JohnBoy2
Forum Newbie
Posts: 19
Joined: Fri Oct 17, 2008 9:31 am

DAO pattern help

Post by JohnBoy2 »

Hello all. My apologies if this is not the best place to post this, but it seemed so after reviewing the categories.

I have a DAO class that I am trying to make orthogonal in supporting transactions. A code snippet paints a thousand words:

Code: Select all

 
  public static function getPersonTestById($value) {
    $retVal = null;
    try {
      $conn = self::getConnection();
      $retVal = self::getPersonTestByIdTrans($value, $conn);
      $conn->close();
    }
    catch (Exception $e) {
      echo "PersonTestDAO::getPersonTestById():  Exception thrown.  Details:  " . $e;
    }
    return $retVal;
  }
 
  public static function getPersonTestByIdTrans($value, $conn) {
    $theSQL = "select * from PersonTests where id = ?;";
 
    $stmt = $conn->prepare($theSQL);
 
    $stmt->bind_param("i", $value);
    $stmt->execute();
 
    if ($conn->error) throw new Exception("PersonTestDAO::getPersonTestByIdTrans():  Exception thrown.  $conn->error: " . $conn->error);
 
    $retVal = self::bindPersonTest($stmt);
 
    $stmt->close();
 
    return $retVal;
  }
 
I'm trying to understand Exceptions, try-catch blocks, and mysqli error reporting all at once, and I'm just not sure I have this right in this case.

Should the code in the getPersonTestByIdTrans() function be wrapped in a try-catch? Or is the $mysqli_error variable the only way that mysqli communicates problems?

Many thanks to all who reply.

John
Last edited by Benjamin on Tue Jun 23, 2009 10:15 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply