pdo-wrapper
pdo-wrapper copied to clipboard
Merge in update() method will omit data
Line 179 - 183:
` //merge data and where together
$collection = array_merge($data, $where);
//collect the values from collection
$values = array_values($collection);`
Merge array $data and $where if they have same columns -> lost data.
The problem will occur when we want to update a column which appear in where (or in array $values). Eg.:
UPDATE contact SET phone = '012-345-6789', first_name='Patrick' WHERE phone = 012-345-9950';
How to solve the problem?
Hi,
Did you resolve this issue?
Bit of an old issue, but updating the 'update' function should resolve this...
public function update($table, $data, $where)
{
//collect the values from data and where
$values = [];
//setup fields
$fieldDetails = null;
foreach ($data as $key => $value) {
$fieldDetails .= "$key = ?,";
$values[] = $value;
}
$fieldDetails = rtrim($fieldDetails, ',');
//setup where
$whereDetails = null;
$i = 0;
foreach ($where as $key => $value) {
$whereDetails .= $i == 0 ? "$key = ?" : " AND $key = ?";
$values[] = $value;
$i++;
}
$stmt = $this->run("UPDATE $table SET $fieldDetails WHERE $whereDetails", $values);
return $stmt->rowCount();
}
Do you want to open a pull request? I'm without my laptop for a few days