0

わかりましたので、PDO と OOP に適応するための学習の旅をゆっくりと続けています。

これが私の問題です。mysql の更新を処理する関数を作成しようとしていますが、非常に複雑に感じられるので、手動で入力したほうがよいでしょう。フォームから大きな更新を処理することが多いので、この関数を再利用できるようにしたかったのですが、複雑すぎると思います。コードを簡単に確認しながら、より簡潔な方法はありますか?

これは私の更新機能です:

 // take data from arrays, loop through and print each out
 // concatenate this onto SET and concatenate the where clause
 // on the end unless there is no criteria in which case print nothing
 public function update_sql($table="users",$update_array,$criteria=""){

 $sql = 'UPDATE `'.$table.'` SET ';
 $sqlFieldParams = array();       
 // creating an array with `user_id` = :user_id etc etc
 foreach ($update_array as $fieldName => $fieldValue) {
     $sqlFieldParams [] = $fieldName . '= :' . $fieldName;
 }

 // concatenate but don't print where if there is no criteria passed
 $sql .= implode(', ', $sqlFieldParams) . ($criteria ? ' WHERE ' . $criteria : "");
 $this->query("$sql");
 }

バインドして実行する私の関数は、バインドが必要な挿入およびその他のステートメントにも使用します。

 public function bind_execute($bind_array){
 // bind the values
 foreach ($bind_array as $field => $item) {
     $this->bind(':'.$field,$item);
 }

 // execute the update
 $this->execute();
 }

参照用にこのスクリプトで使用されている、さらにいくつかの再利用可能な関数

    // prepare our SQL Queries
public function query($query){
    $this->stmt = $this->dbh->prepare($query);
}

// use switch to select the appropriate type for the value been passed
// $param = placeholder name e.g username, $value = myusername
public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
// run the binding process
$this->stmt->bindValue($param, $value, $type);
}

// execute the prepared statement
public function execute(){
    return $this->stmt->execute();
}

そして今、私の巨大な更新ステートメント

 $this->user_id = $_GET['id'];
 $this->user_activation_hash = $_GET['verification_code'];

 // create an array to pass these values to be set to the update function
 $update_array = array(
 'user_active'               => '1',
 'user_activation_hash'      => 'NULL',
 );

 // create the where clause
 $criteria = 'user_id = :user_id AND user_activation_hash = :user_activation_hash';

 // create the update statement
 // pass in values table, array & criteria
 $database->update_sql('users',$update_array,$criteria);

 // these are other values that need binding from the where clause
 $criteria_array = array(
    'user_id'                => "'.$this->user_id.'"
 );

 // join the set values of the update with the where values
 // in the one array to merge then in a for loop next
 $bind_array = array_merge($update_array, $criteria_array);

 $database->bind_execute($bind_array);

感想、フィードバック?より良いアプローチ?あなたがそれを取り除けば、たったの5行だと思いますが、私はそれを複雑にしすぎたのではないでしょうか?

4

1 に答える 1