lollipop

A PHP-framework
Log | Files | Refs

commit f61c789548b74fbcd8fd5c1aadf49aab1b24759e
parent b17d77c4afc5c792c49af2b7a44155ce780549ba
Author: Friedel Schon <[email protected]>
Date:   Thu,  6 Apr 2023 11:51:12 +0200

basic orm implementation

Diffstat:
Morm.php | 97++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Atest_orm.php | 28++++++++++++++++++++++++++++
2 files changed, 112 insertions(+), 13 deletions(-)

diff --git a/orm.php b/orm.php @@ -4,39 +4,110 @@ namespace Lollipop; use mysqli; -class DatabaseObject +abstract class DatabaseObject { - protected SQLDatabase $db; protected string $table; + protected string $primary; + + protected SQLDatabase $db; + protected array $data; + protected array $changed_keys; - function __construct(SQLDatabase $db, string $table) + function __construct(SQLDatabase $db) { $this->db = $db; - $this->table = $table; + $this->primary = $this->get_primary(); + $this->table = $this->get_table(); } - function select($x) + abstract static function get_primary(): string; + abstract static function get_table(): string; + + public function load(string $id): bool { + $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?"; + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param("s", $id); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows == 0) { + return false; + } + + $this->data = $result->fetch_assoc(); + return true; + } + + public function save() + { + if (!$this->changed_keys) + return; + + $sql = "UPDATE {$this->table} SET "; + + $values = []; + $types = ""; + foreach ($this->changed_keys as $index => $key) { + if ($index > 0) + $sql .= ', '; + $sql .= "$key = ?"; + $values[] = $this->data[$key]; + $types .= 's'; + } + + $sql .= " WHERE $this->primary = ?"; + $values[] = $this->data[$this->primary]; + $types .= 's'; + + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param($types, ...$values); + $stmt->execute(); + + $this->changed_keys = []; + } + + public function delete() + { + $sql = "DELETE FROM {$this->table} WHERE {$this->primary} = ?"; + $stmt = $this->db->conn->prepare($sql); + $stmt->bind_param("s", $this->data[$this->primary]); + $stmt->execute(); + $this->data = []; + $this->changed_keys = []; + } + + public function __get(string $name) + { + return $this->data[$name]; + } + + public function __set(string $name, $value) + { + $this->data[$name] = $value; + $this->changed_keys[] = $name; + } + + public function getData() + { + return $this->data; } } class SQLDatabase { - protected mysqli $conn; + public mysqli $conn; function __construct(string $host, string $username, string $password, string $database = null, int $port = null) { $this->conn = new mysqli($host, $username, $password, $database, $port); } - function register(string $class) - { - get_class() - } - - function get(string $table): DatabaseObject + function get(string $table_class, $name) { - return new DatabaseObject($this, $table); + $cls = new $table_class($this); + $cls->load($name); + return $cls; } } \ No newline at end of file diff --git a/test_orm.php b/test_orm.php @@ -0,0 +1,27 @@ +<?php +use Lollipop\DatabaseObject; + +require_once "orm.php"; + +class User extends DatabaseObject +{ + static function get_table(): string + { + return "user"; + } + + static function get_primary(): string + { + return "email"; + } +} + +$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); + +$u = $db->get(User::class, "[email protected]"); + +echo $u->fname; + +$u->fname = "Harald"; + +$u->save(); +\ No newline at end of file