/** * Updates multiple rows. The values for the column to search for is the * key of each row. * The number of total affected rows can be found in * $this->multipleAffectedRows. * * @param string $tableName The table name. * @param array[] $data The array of data changes. An array * indexed by the value of the column to * apply the update to (typically the primary) * key containing * an array of column-value pairs to update * for that row. * e.g. * [ * 1 => [ * 'col1' => 'newStringValue', * 'col2' => 'newStringValue2' * ], * 2 => [ * 'id' => 2, * 'col1' => 'anotherNewStringValue', * 'col2' => 'anotherNewStringValue2' * ], * ... * ] * @param string $column (Options) the name of the column and * key to search for. The default is * 'id'. * @param boolean $updateSameColumn (Optional) If the column being searched * for is within the a data row, * if this is false, that key should * be removed before updating the data. * This is the default. * * @return boolean **/ public function updateMultipleByKey($tableName, array $data, $column = 'id', $updateSameColumn = false) { $successesNeeded = count($data); $where = $this->escapeColumn($column) . " = ?"; $affectedRows = 0; foreach ($data as $by => $row) { if (!$updateSameColumn) { unset($row[$column]); } $stmt = new AMysql_Statement($this); $stmt->update($tableName, $row, $where)->execute(array($by)); $affectedRows += $stmt->affectedRows; if ($stmt->result) { $successesNeeded--; } } $this->multipleAffectedRows = $affectedRows; return 0 === $successesNeeded; }