PDOWrapper::insertMultiple PHP Метод

insertMultiple() публичный Метод

- adds multiple rows to a table with a single query
public insertMultiple ( string $table, array $columns = [], $rows = [], boolean $timestamp_these = null ) : mixed
$table string - the name of the db table we are adding row to
$columns array - contains the column names
$timestamp_these boolean (Optional), if true we set date_created and date_modified values to NOW() for each row
Результат mixed - new primary key of inserted table, false on failure
    public function insertMultiple($table, $columns = array(), $rows = array(), $timestamp_these = null)
    {
        if (is_null($timestamp_these)) {
            $timestamp_these = self::$TIMESTAMP_WRITES;
        }
        // generate the columns portion of the insert statment
        // adding the timestamp fields if needs be
        if ($timestamp_these) {
            $columns[] = 'date_created';
            $columns[] = 'date_modified';
        }
        $columns_str = '(' . implode(',', $columns) . ') ';
        // generate the values portions of the string
        $values_str = 'VALUES ';
        $add_comma = false;
        foreach ($rows as $row_index => $row_values) {
            // only add comma after the first row has been added
            if ($add_comma) {
                $values_str .= ', ';
            } else {
                $add_comma = true;
            }
            // here we will create the values string for a single row
            $values_str .= '(';
            $add_comma_forvalue = false;
            foreach ($row_values as $value_index => $value) {
                if ($add_comma_forvalue) {
                    $values_str .= ', ';
                } else {
                    $add_comma_forvalue = true;
                }
                // generate the bind variable name based on the row and column index
                $values_str .= ':' . $row_index . '_' . $value_index;
            }
            // append timestamps if necessary
            if ($timestamp_these) {
                $values_str .= ($add_comma_forvalue ? ', ' : '') . time() . ', ' . time();
            }
            $values_str .= ')';
        }
        // build final insert string
        $sql_str = "INSERT INTO {$table} {$columns_str} {$values_str}";
        // now we attempt to write this multi inster query to the database using a transaction
        try {
            $this->getMaster()->beginTransaction();
            $pstmt = $this->getMaster()->prepare($sql_str);
            // traverse the 2d array of rows and values to bind all parameters
            foreach ($rows as $row_index => $row_values) {
                foreach ($row_values as $value_index => $value) {
                    $pstmt->bindValue(':' . $row_index . '_' . $value_index, $value);
                }
            }
            // now lets execute the statement, commit the transaction and return
            $pstmt->execute();
            $this->getMaster()->commit();
            return true;
        } catch (PDOException $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            $this->getMaster()->rollback();
            return false;
        } catch (Exception $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            $this->getMaster()->rollback();
            return false;
        }
    }