1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- /**
- * Created by PhpStorm.
- * User: root
- * Date: 31.07.18
- * Time: 10:49
- */
- require_once "DB.php";
- require_once "Author.php";
- require_once "Book.php";
- require_once "User.php";
- abstract class Model
- {
- public static $table;
- public static $fields;
- public static $pk;
- public $connection;
- public function __construct()
- {
- $this->connection = DB::getConnection();
- }
- public function getByPk($id): array
- {
- $query =
- "SELECT " . implode(self::$fields, ", ") .
- " FROM " . self::$table .
- " WHERE " . self::$pk . " = " . $id;
- return $this->connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
- }
- public function getAllRows(): array
- {
- $query =
- "SELECT * FROM " . self::$table;
- return $this->connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
- }
- public function getRowsByField(array $fields): array
- {
- $query =
- "SELECT `" . implode("`, `", $fields) . "`" .
- " FROM " . self::$table;
- return $this->connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
- }
- public function deleteRow(string $condition): void
- {
- $query =
- "DELETE FROM " . self::$table .
- " WHERE " . $condition;
- $this->connection->exec($query);
- }
- public function updateRow(string $targetSet, string $condition): void
- {
- $query =
- "UPDATE " . self::$table .
- " SET " . $targetSet .
- " WHERE " . $condition;
- $this->connection->exec($query);
- }
- }
|