-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
99 lines (77 loc) · 3.16 KB
/
example.php
File metadata and controls
99 lines (77 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
require_once '../../vendor/autoload.php';
require_once __DIR__.'/Domain/User.php';
require_once __DIR__.'/Infrastructure/Schema/UserTable.php';
require_once __DIR__.'/Infrastructure/Repository/UserRepository.php';
use Domain\User;
use Infrastructure\Repository\UserRepository;
use Infrastructure\Schema\UserTable;
use WebFiori\Database\Attributes\AttributeTableBuilder;
use WebFiori\Database\ConnectionInfo;
use WebFiori\Database\Database;
const SEP = "────────────────────────────────────────────────────────────────────\n";
echo "=== WebFiori Database Clean Architecture Example ===\n\n";
echo "Architecture layers:\n";
echo " - Domain: Pure entities (User.php)\n";
echo " - Infrastructure/Schema: Table definitions with attributes (UserTable.php)\n";
echo " - Infrastructure/Repository: Data access with AbstractRepository\n\n";
try {
$connection = new ConnectionInfo('mysql', 'root', '123456', 'testing_db');
$database = new Database($connection);
echo SEP;
echo "1. Building Table from Attributes:\n";
$table = AttributeTableBuilder::build(UserTable::class, 'mysql');
echo " ✓ Table blueprint built from UserTable attributes\n";
echo " Columns: ".implode(', ', array_keys($table->getCols()))."\n\n";
echo SEP;
echo "2. Creating Table:\n";
$database->addTable($table);
$database->table('users')->drop(true)->execute();
$database->createTables();
echo " ✓ Users table created\n\n";
echo SEP;
echo "3. Using Repository (extends AbstractRepository):\n";
$userRepo = new UserRepository($database);
echo " ✓ UserRepository created\n\n";
echo SEP;
echo "4. Saving Domain Entities:\n";
$users = [
new User(null, 'Ahmed Ali', 'ahmed@example.com', 28),
new User(null, 'Sara Hassan', 'sara@example.com', 35),
new User(null, 'Omar Khalil', 'omar@example.com', 22)
];
foreach ($users as $user) {
$userRepo->save($user);
echo " ✓ Saved: {$user->name}\n";
}
echo "\n";
echo SEP;
echo "5. Repository Operations:\n";
$all = $userRepo->findAll();
echo " All users (".count($all)."):\n";
foreach ($all as $u) {
echo " - {$u->name} ({$u->email}) - Age: {$u->age}\n";
}
$user = $userRepo->findById(1);
echo "\n Find by ID 1: {$user->name}\n";
$adults = $userRepo->findByAge(25);
echo "\n Users age >= 25 (".count($adults)."):\n";
foreach ($adults as $u) {
echo " - {$u->name} (Age: {$u->age})\n";
}
$page = $userRepo->paginate(1, 2);
echo "\n Page 1 (2 per page): {$page->getTotalItems()} total, {$page->getTotalPages()} pages\n";
echo "\n Total count: ".$userRepo->count()."\n\n";
echo SEP;
echo "6. Cleanup:\n";
$database->table('users')->drop()->execute();
echo " ✓ Table dropped\n";
} catch (Exception $e) {
echo "✗ Error: ".$e->getMessage()."\n";
try {
$database->table('users')->drop(true)->execute();
} catch (Exception $cleanupError) {
}
}
echo "\n".SEP;
echo "=== Example Complete ===\n";