From a84232811dadccf7047424f27340a7f9847bedff Mon Sep 17 00:00:00 2001 From: winter Date: Sat, 7 Dec 2024 21:44:42 +0000 Subject: many many changes but actors are loaded from db now --- WpfTest/Model.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 WpfTest/Model.php (limited to 'WpfTest/Model.php') diff --git a/WpfTest/Model.php b/WpfTest/Model.php new file mode 100644 index 0000000..26a7b49 --- /dev/null +++ b/WpfTest/Model.php @@ -0,0 +1,73 @@ +getProperties(); + $result = []; + foreach ($props as $p) { + $result[self::mangleName($p->getName())] = $p->getValue(); + } + return $result; + } + + public static function fromDbRow(array|false $columns): ?static { + if ($columns === false) + return null; + $obj = new static(); + $refl = new \ReflectionObject($obj); + foreach (array_keys($columns) as $name) { + $value = $columns[$name]; + $name = self::unmangleName($name); + try { + $obj->{$name} = $value; + } catch (\TypeError $e) { + $type = $refl->getProperty($name)->getType(); + if ($type == null || !is_a($type, \ReflectionNamedType::class)) + throw $e; + $obj->{$name} = new ($type->getName())($value); + } + } + return $obj; + } + + public static function findWhere(string $whereClause, array $parameters): ?static { + $classNameParts = explode('\\', static::class); + $className = $classNameParts[count($classNameParts) - 1]; + $tableName = self::mangleName($className); + $stmt = Db::getInstance()->getPdo()->prepare("SELECT * FROM $tableName WHERE $whereClause"); + $stmt->execute($parameters); + return static::fromDbRow($stmt->fetch(\PDO::FETCH_ASSOC)); + } + + public static function find(int $id): ?static { + return static::findWhere('id = ?', [$id]); + } +} \ No newline at end of file -- cgit v1.3