lollipop

A PHP-framework
Log | Files | Refs

commit fae9d11f34cc60b1852740158cea5bd0426655d6
parent 26b8a5c80357c85ed682a4d4732a6b2de543e3d3
Author: Friedel Schon <[email protected]>
Date:   Tue, 11 Apr 2023 13:05:18 +0200

SQLDatabase::all()

Diffstat:
MLollipop/SQLDatabase.php | 19+++++++++++++++++++
Mtest_orm.php | 16++++++----------
2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/Lollipop/SQLDatabase.php b/Lollipop/SQLDatabase.php @@ -18,6 +18,25 @@ namespace Lollipop { $cls->load($name); return $cls; } + + function all(string $table_name) + { + $cls = new $table_name($this); + + $sql = "SELECT {$cls->get_primary()} FROM {$cls->get_table()}"; + + $result = $this->conn->query($sql); + + if (!$result || $result->num_rows == 0) { + return []; + } + + $objects = []; + while ($row = $result->fetch_assoc()) { + $objects[] = $this->get($table_name, $row[$cls->get_primary()]); + } + return $objects; + } } } ?> \ No newline at end of file diff --git a/test_orm.php b/test_orm.php @@ -1,15 +1,11 @@ <?php -include "autoloader.php"; +include "utils/autoloader.php"; $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); -$u = $db->get(User::class, "[email protected]"); -$p = $db->get(Permission::class, "[email protected]"); +$u = $db->all(User::class); -echo $p->id; -echo $u->fname; - -$u->fname = "Harald"; - -$u->save(); -\ No newline at end of file +foreach ($u as $c) { + var_dump($c); +} +\ No newline at end of file