public function insertOrUpdate($table, array $data)
{
// extract and quote col names from the array keys
$i = 0;
$bind = [];
$cols = [];
$vals = [];
foreach ($data as $col => $val) {
$cols[] = $this->quoteIdentifier($col, true);
if ($val instanceof \Zend_Db_Expr) {
$vals[] = $val->__toString();
} else {
if ($this->supportsParameters('positional')) {
$vals[] = '?';
$bind[] = $val;
} else {
if ($this->supportsParameters('named')) {
$bind[':col' . $i] = $val;
$vals[] = ':col' . $i;
$i++;
} else {
/** @see \Zend_Db_Adapter_Exception */
throw new \Zend_Db_Adapter_Exception(get_class($this->getResource()) . " doesn't support positional or named binding");
}
}
}
}
// build the statement
$set = [];
foreach ($cols as $i => $col) {
$set[] = sprintf('%s = %s', $col, $vals[$i]);
}
$sql = sprintf('INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s;', $this->quoteIdentifier($table, true), implode(', ', $cols), implode(', ', $vals), implode(', ', $set));
// execute the statement and return the number of affected rows
if ($this->supportsParameters('positional')) {
$bind = array_values($bind);
}
$bind = array_merge($bind, $bind);
$stmt = $this->query($sql, $bind);
$result = $stmt->rowCount();
return $result;
}