diff options
author | Camil Staps | 2016-07-27 17:28:46 +0200 |
---|---|---|
committer | Camil Staps | 2016-07-27 17:28:46 +0200 |
commit | 411f03fb4643cdf7afd9ef1a3fcc2142ad86f1bf (patch) | |
tree | aab2a782a31064c6bce716ad72241f1957ea80aa /classes | |
parent | File: use Model (diff) |
Model: __set() cannot return a value, new exception instead
Diffstat (limited to 'classes')
-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; } /** |