PEAR::AuthにPDOのContainerを追加してみる(検証中)その2

PEAR::AuthにPDOのContainerを追加してみる(検証中)の続きです。

addUser()を使ってテーブルにユーザー登録はできたのですが、
同じユーザー名を入れられないようにするにはどうしたらいいかなと思いまして。

1.addUserする前に検索して1つあるかどうかを探す。
2.エラーをキャッチして表示する。

1は例があったので、2にしてみました。(あまのじゃく)

515
516
517
518
519
520
521
        try{
            $sth->execute();
            $res = $sth->fetch(PDO::FETCH_ASSOC);
        }catch(PDOException $e){
            //var_dump($e);
            return PEAR::raiseError($e->getMessage());
        } // try

こんな感じでエラーをキャッチします。var_dumpで見たときはこんな出力でした。

object(PDOException)#6 (8) {
 ["message":protected]=> string(175) "SQLSTATE[23505]: Unique violation: 7 ERROR: 重複キーが一意性制約"player_name_uniq"に違反しています DETAIL: キー (name)=(test) はすでに存在します" 
 ["string":"Exception":private]=> string(0) "" 
 ["code":protected]=> string(5) "23505" 
 ["file":protected]=> string(54) "/usr/xxxxxxxxxxxxxxxlib/pear.auth.PDO.class.php" 
 ["line":protected]=> int(516) 
 ["trace":"Exception":private]=> array(3) {
    [0]=> array(6) { 
      ["file"]=> string(54) "/usr/xxxxxxxxxxxxxxx/lib/pear.auth.PDO.class.php" 
      ["line"]=> int(516) 
      ["function"]=> string(7) "execute" 
      ["class"]=> string(12) "PDOStatement" 
      ["type"]=> string(2) "->" 
      ["args"]=> array(0) { } } 
    [1]=> array(6) {
      ["file"]=> string(24) "/usr/share/pear/Auth.php" 
      ["line"]=> int(1213) 
      ["function"]=> string(7) "addUser" 
      ["class"]=> string(18) "Auth_Container_PDO" 
      ["type"]=> string(2) "->" 
      ["args"]=> array(3) { 
        [0]=> &string(4) "test" 
        [1]=> &string(4) "test" 
        [2]=> &string(0) "" } } 
    [2]=> array(6) { 
      ["file"]=> string(51) "/usr/xxxxxxxxxxxxxxx/htdocs/xxxxxx/xxxxx.php" 
      ["line"]=> int(30) 
      ["function"]=> string(7) "addUser" 
      ["class"]=> string(4) "Auth" 
      ["type"]=> string(2) "->" 
      ["args"]=> array(2) { 
        [0]=> &string(4) "test" 
        [1]=> &string(4) "test" } } }
 ["previous":"Exception":private]=> NULL 
 ["errorInfo"]=> array(3) { 
   [0]=> string(5) "23505" 
   [1]=> int(7) 
   [2]=> string(138) "ERROR: 重複キーが一意性制約"player_name_uniq"に違反しています DETAIL: キー (name)=(test) はすでに存在します" } }

この出力の中で、この行を入れることで簡単にメッセージが抜けます。
520 return PEAR::raiseError($e->getMessage());
※getMessageだけじゃなくて、getCodeとかもあるので、これらの中から自由に取れるかも。

http://php.net/manual/ja/language.exceptions.phpに書いてあった例です。

    /* Protected methods inherited from Exception class */
    public function getMessage();                 // Exception message
    public function getCode();                    // User-defined Exception code
    public function getFile();                    // Source filename
    public function getLine();                    // Source line
    public function getTrace();                   // An array of the backtrace()
    public function getTraceAsString();           // Formated string of trace

プログラムでライブラリを呼んだ時の動作です。

if(isset($_POST['username']) && isset($_POST['password'])){
  echo '登録しています<br>';
   $out=$auth_obj->addUser($_POST['username'],$_POST['password']);
   //echo $out.'<br>'; // debug
 
  if(stristr($out,'SQLSTATE[23505]')){
    echo $_POST['username'].'はすでに使用されています。別のIDを入力してください。<br>';
  }else{
    echo $_POST['username'].'は正常に登録されました。<br>';
  } // if SQLSTATE[23505]
 
} // if $_GET check