diff options
Diffstat (limited to 'classes/Model.php')
-rw-r--r-- | classes/Model.php | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/classes/Model.php b/classes/Model.php index 3e99856..792027c 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -34,6 +34,12 @@ class ModelIllegalAccessException extends Exception { } /** + * Thrown in Model when a call to __set() fails + */ +class ModelSetFailedException { +} + +/** * An abstract interface to a database table */ abstract class Model { @@ -82,17 +88,23 @@ abstract class Model { * @param mixed $value The value * * @throws PDOException Database error - * - * @return bool True iff the value has been changed */ public function __set($key, $value) { if (!in_array($key, $this->fillable_columns)) { throw new ModelIllegalAccessException("Column $key cannot be edited."); } + if ($this->data[$key] == $value) { + return; + } $stmt = $this->pdo->prepare("UPDATE `".$this->table()."` SET `$key`=? WHERE `{$this->primary_key}`=?"); - $stmt->execute([$this->mutator($value), $this->data[$this->primary_key]]); + $stmt->execute([ + $this->mutator($key, $value), + $this->data[$this->primary_key] + ]); + if ($stmt->rowCount() != 1) { + throw new ModelEditFailedException(); + } $this->data[$key] = $value; - return $this->rowCount() != 0; } /** |