public function insert($tablename, &$data, $modifiers = '', $useMaster = true, $action = 'INSERT')
{
if (!in_array($action, array('INSERT', 'REPLACE'))) {
$action = 'INSERT';
}
$tablename = '`' . str_replace('.', '`.`', trim($tablename, '"\'`')) . '`';
$query = $action . ' ' . $modifiers . ' INTO ' . $tablename;
if ($useMaster) {
$dbh =& self::$mdbh;
} else {
$dbh =& $this->dbh;
}
$insertCnt = 0;
// Check for bulk insert
if (is_array($data[key($data)])) {
$recCount = count($data);
// Get Field List
$fields = array_keys($data[key($data)]);
// Create field string
$ifields = '`' . implode('`,`', $fields) . '`';
$batches = ceil($recCount / $this->bulkInsertSize) . "\n";
$unprepared = true;
// Loop through inserts in batches
for ($b = 1; $b <= $batches; $b++) {
$bindings = array();
$istart = ($b - 1) * $this->bulkInsertSize;
$iend = min($this->bulkInsertSize, $recCount - $istart);
$idata = array();
$bindings = array();
// Create bindings
$data_block = array_slice($data, $istart, $this->bulkInsertSize);
$i = 0;
$bcnt = 0;
foreach ($data_block as $rec) {
foreach ($rec as $field => $val) {
$idata[$field . $i] = $val;
}
$bindings[] = '(:' . implode($i . ',:', $fields) . $i . ')';
$bcnt += count($fields);
$i++;
}
// Check if a prepare statement needs to be run (first and last inserts)
if ($unprepared || $b == $batches) {
$pquery = $query . ' (' . $ifields . ') VALUES ' . implode(',', $bindings);
$this->prepare($pquery, $useMaster);
$unprepared = false;
}
$iresult = $this->execute($idata, $useMaster);
if ($iresult > 0) {
$insertCnt += $iresult;
} else {
return $insertCnt;
}
}
$this->insertId = $dbh->lastInsertId();
} else {
$fields = array_keys($data);
$fieldCnt = count($data);
$query .= ' (`' . implode('`,`', $fields) . '`) VALUES ( :' . implode(', :', $fields) . ' )';
$this->prepare($query, $useMaster);
if ($this->execute($data, $useMaster) == 1) {
$insertCnt++;
$this->insertId = $dbh->lastInsertId();
}
}
return $insertCnt;
}