diff options
Diffstat (limited to 'classes/Model.php')
-rw-r--r-- | classes/Model.php | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/classes/Model.php b/classes/Model.php index 3b1c49e..9ad86f8 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -52,7 +52,7 @@ abstract class Model { * @var string[] $dates Columns that are DATEs (special treatment in accessor and mutator) * @var string[] $booleans Columns that are BOOLEANs (special treatment in accessor and mutator) */ - public + public static $table = '', $primary_key = 'id', $protected_columns = ['id'], @@ -79,10 +79,10 @@ abstract class Model { public function __construct($pdo, $id) { $this->pdo = $pdo; - $stmt = $this->pdo->prepare("SELECT * FROM `".$this->table()."` WHERE `{$this->primary_key}`=?"); + $stmt = $this->pdo->prepare("SELECT * FROM `".self::table()."` WHERE `".static::$primary_key."`=?"); $stmt->execute([$id]); if ($stmt->rowCount() == 0) { - throw new ModelNotFoundException("The {$this->table} with id '$id' could not be found."); + throw new ModelNotFoundException("The ".static::$table." with id '$id' could not be found."); } $this->data = $stmt->fetch(PDO::FETCH_ASSOC); } @@ -96,19 +96,19 @@ abstract class Model { * @throws PDOException Database error */ public function __set($key, $value) { - if (!in_array($key, $this->fillable_columns)) { - throw new ModelIllegalAccessException("Column `{$this->table()}`.`$key` cannot be edited."); + if (!in_array($key, static::$fillable_columns)) { + throw new ModelIllegalAccessException("Column `".self::table()."`.`$key` cannot be edited."); } if ($this->data[$key] == $value) { return; } - $stmt = $this->pdo->prepare("UPDATE `".$this->table()."` SET `$key`=? WHERE `{$this->primary_key}`=?"); + $stmt = $this->pdo->prepare("UPDATE `".self::table()."` SET `$key`=? WHERE `".static::$primary_key."`=?"); $stmt->execute([ $this->mutator($key, $value), - $this->data[$this->primary_key] + $this->data[static::$primary_key] ]); if ($stmt->rowCount() != 1) { - throw new ModelEditFailedException("Failed to update `{$this->table()}`.`$key` to '$value'."); + throw new ModelEditFailedException("Failed to update `".self::table()."`.`$key` to '$value'."); } $this->data[$key] = $value; } @@ -125,6 +125,35 @@ abstract class Model { } /** + * Create a new row + * + * @param PDO $pdo Database connection + * @param mixed[] $values The column values, in the order of $fillable_columns + * + * @throws PDOException Database error + * + * @return self The new item + */ + public static function create($pdo, $values) { + $columns = array_combine(static::$fillable_columns, $values); + $questions = []; + + foreach ($columns as $column => $value) { + $columns[$column] = self::mutator($column, $value); + $questions[] = '?'; + } + + $stmt = $pdo->prepare( + "INSERT INTO `".self::table()."` " . + "(`" . implode('`, `', array_keys($columns)) . "`) " . + "VALUES (" . implode(',', $questions) . ")"); + $stmt->execute(array_values($columns)); + + $class = get_called_class(); + return new $class($pdo, $pdo->lastInsertId()); + } + + /** * Post-__get() hook to modify the value * * @param string $key The column @@ -132,12 +161,12 @@ abstract class Model { * * @return mixed The modified value */ - protected function accessor($key, $value) { + protected static function accessor($key, $value) { if (is_null($value)) { return null; - } elseif (in_array($key, $this->booleans)) { + } elseif (in_array($key, static::$booleans)) { return (bool) $value; - } elseif (in_array($key, $this->dates) || in_array($key, $this->timestamps)) { + } elseif (in_array($key, static::$dates) || in_array($key, static::$timestamps)) { return strtotime($value); } else { return $value; @@ -152,10 +181,10 @@ abstract class Model { * * @return string The modified value */ - protected function mutator($key, $value) { - if (in_array($key, $this->dates) && is_int($value)) { + protected static function mutator($key, $value) { + if (in_array($key, static::$dates) && is_int($value)) { return date('Y-m-d', $value); - } elseif (in_array($key, $this->timestamps) && is_int($value)) { + } elseif (in_array($key, static::$timestamps) && is_int($value)) { return date('Y-m-d H:i:s', $value); } else { return (string) $value; @@ -170,8 +199,8 @@ abstract class Model { * @return bool True iff the row was really deleted */ public function delete() { - $stmt = $this->pdo->prepare("DELETE FROM `{$this->table()}` WHERE `{$this->primary_key}`=?"); - $stmt->execute([$this->data[$this->primary_key]]); + $stmt = $this->pdo->prepare("DELETE FROM `".self::table()."` WHERE `".static::$primary_key."`=?"); + $stmt->execute([$this->data[static::$primary_key]]); return $stmt->rowCount() != 0; } @@ -180,7 +209,7 @@ abstract class Model { * * @return string The database table */ - private function table() { - return Constants::db_prefix . $this->table; + private static function table() { + return Constants::db_prefix . static::$table; } } |