lollipop

A PHP-framework
Log | Files | Refs

commit b7bf7de87542026ddd7611ecfdbd67034ada9748
parent 163d174aa7d3f6a536454cc158f88a6ea37e0a8d
Author: Friedel Schon <[email protected]>
Date:   Tue, 11 Apr 2023 13:53:45 +0200

where -> setData()

Diffstat:
MLollipop/DatabaseObject.php | 9+++++++--
MLollipop/SQLDatabase.php | 7+++++--
2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -9,8 +9,8 @@ namespace Lollipop { protected string $primary; protected SQLDatabase $db; - protected array $data; - protected array $changed_keys; + protected array $data = []; + protected array $changed_keys = []; function __construct(SQLDatabase $db) { @@ -22,6 +22,11 @@ namespace Lollipop { abstract static function get_primary(): string; abstract static function get_table(): string; + public function setData($data) + { + $this->data = $data; + } + public function load(string $id): bool { $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?"; diff --git a/Lollipop/SQLDatabase.php b/Lollipop/SQLDatabase.php @@ -26,7 +26,7 @@ namespace Lollipop { } $cls = new $table_name($this); - $sql = "SELECT {$cls->get_primary()} FROM {$cls->get_table()} WHERE "; + $sql = "SELECT * FROM {$cls->get_table()} WHERE "; $params = []; $i = 0; @@ -49,7 +49,9 @@ namespace Lollipop { $objects = []; while ($row = $result->fetch_assoc()) { - $objects[] = $this->get($table_name, $row[$cls->get_primary()]); + $o = new $table_name($this); + $o->setData($row); + $objects[] = $o; } return $objects; } @@ -57,6 +59,7 @@ namespace Lollipop { function all(string $table_name) { $cls = new $table_name($this); + $sql = "SELECT {$cls->get_primary()} FROM {$cls->get_table()}"; $result = $this->conn->query($sql);