/** * Updates the table with data from the data model. * * @param $table * @param $update * @param $where * @return \Common\Storage\PDOStatement * @throws \InvalidArgumentException */ public function autoUpdate($table, $update, $where) { if (empty($table)) { throw new \InvalidArgumentException("Table being referenced must be set when using autoUpdate"); } if (empty($update)) { throw new \InvalidArgumentException("variables to be inserted must be set when using autoUpdate"); } if (empty($where)) { throw new \InvalidArgumentException('a where clause is expected to be set when using autoUpdate'); } if (is_object($update)) { $update = Introspection::flattenToArray($update); } $skipValues = array(); foreach ($update as $columnName => $value) { if ($value === "NOW()" || $value === "CURDATE()" || $value === "NULL") { $set[] = "{$columnName} = {$value}"; $skipValues[] = $columnName; } else { $set[] = '`' . $columnName . '` = ?'; } } // need remove and null values from the update param s foreach ($skipValues as $skipValue) { unset($update[$skipValue]); } $params = array_merge(array_values($update), array_values($where)); $query = 'UPDATE ' . $table . ' SET ' . implode(", ", $set) . ' WHERE ' . implode(' = ? AND ', array_keys($where)) . ' = ? '; try { $Stmt = $this->prepare($query); } catch (\Exception $e) { throw QueryException::create("Update Query Prepare failed: " . $e->getMessage())->setQuery($this->getLastQuery()); } try { $Stmt->execute(array_values($params)); } catch (\PDOException $e) { switch ($e->getCode()) { case '23000': return $Stmt; case '40001': $Stmt->execute(array_values($params)); break; default: throw $e; } } return $Stmt; }