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;
}
}