aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-07-27 17:28:46 +0200
committerCamil Staps2016-07-27 17:28:46 +0200
commit411f03fb4643cdf7afd9ef1a3fcc2142ad86f1bf (patch)
treeaab2a782a31064c6bce716ad72241f1957ea80aa
parentFile: use Model (diff)
Model: __set() cannot return a value, new exception instead
-rw-r--r--classes/Model.php20
-rw-r--r--include/clients-edit.php9
2 files changed, 18 insertions, 11 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;
}
/**
diff --git a/include/clients-edit.php b/include/clients-edit.php
index 00961aa..ded72d2 100644
--- a/include/clients-edit.php
+++ b/include/clients-edit.php
@@ -24,13 +24,8 @@ $response = new Response();
try {
$client = new Client($_pdo, $_REQUEST['pk']);
- if ($client->setName($_REQUEST['value'])) {
- $response->success = true;
- } else {
- $response->http_response_code(500);
- $response->success = false;
- $response->message = "The client could not be edited due to an error.";
- }
+ $client->name = $_REQUEST['value'];
+ $response->success = true;
} catch (PDOException $e) {
$response->http_response_code(500);
$response->success = false;