-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
93 lines (71 loc) · 3.28 KB
/
example.php
File metadata and controls
93 lines (71 loc) · 3.28 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
<?php
require_once '../../vendor/autoload.php';
use WebFiori\Database\ColOption;
use WebFiori\Database\ConnectionInfo;
use WebFiori\Database\Database;
use WebFiori\Database\DataType;
const SEP = "────────────────────────────────────────────────────────────────────\n";
echo "=== WebFiori Database Table Blueprints Example ===\n\n";
try {
$connection = new ConnectionInfo('mysql', 'root', '123456', 'testing_db');
$database = new Database($connection);
echo SEP;
echo "1. Creating Users Table Blueprint:\n";
$usersTable = $database->createBlueprint('users')->addColumns([
'id' => [ColOption::TYPE => DataType::INT, ColOption::PRIMARY => true, ColOption::AUTO_INCREMENT => true],
'username' => [ColOption::TYPE => DataType::VARCHAR, ColOption::SIZE => 50],
'email' => [ColOption::TYPE => DataType::VARCHAR, ColOption::SIZE => 150],
'created-at' => [ColOption::TYPE => DataType::TIMESTAMP, ColOption::DEFAULT => 'current_timestamp']
]);
echo " ✓ Users table blueprint created\n";
echo " Columns: ".implode(', ', $usersTable->getColsKeys())."\n\n";
echo SEP;
echo "2. Creating Posts Table Blueprint:\n";
$postsTable = $database->createBlueprint('posts')->addColumns([
'id' => [ColOption::TYPE => DataType::INT, ColOption::PRIMARY => true, ColOption::AUTO_INCREMENT => true],
'user-id' => [ColOption::TYPE => DataType::INT],
'title' => [ColOption::TYPE => DataType::VARCHAR, ColOption::SIZE => 200],
'content' => [ColOption::TYPE => DataType::TEXT],
'created-at' => [ColOption::TYPE => DataType::TIMESTAMP, ColOption::DEFAULT => 'current_timestamp']
]);
echo " ✓ Posts table blueprint created\n";
echo " Columns: ".implode(', ', $postsTable->getColsKeys())."\n\n";
echo SEP;
echo "3. Adding Foreign Key Relationship:\n";
$postsTable->addReference($usersTable, ['user-id' => 'id'], 'user_fk', 'cascade', 'cascade');
echo " ✓ Foreign key added (posts.user-id -> users.id)\n\n";
echo SEP;
echo "4. Creating Tables:\n";
$database->table('users')->drop(true)->execute();
$database->table('posts')->drop(true)->execute();
$database->createTables();
echo " ✓ Tables created\n\n";
echo SEP;
echo "5. Testing Tables:\n";
$database->table('users')->insert([
'username' => 'ahmad_salem',
'email' => 'ahmad@example.com'
])->execute();
echo " ✓ Inserted test user\n";
$database->table('posts')->insert([
'user-id' => 1,
'title' => 'My First Post',
'content' => 'This is the content of my first post.'
])->execute();
echo " ✓ Inserted test post\n";
$result = $database->table('users')->select()->execute();
echo " Users:\n";
foreach ($result as $row) {
echo " - {$row['username']} ({$row['email']})\n";
}
echo "\n";
echo SEP;
echo "6. Cleanup:\n";
$database->table('posts')->drop()->execute();
$database->table('users')->drop()->execute();
echo " ✓ Tables dropped\n";
} catch (Exception $e) {
echo "✗ Error: ".$e->getMessage()."\n";
}
echo "\n".SEP;
echo "=== Example Complete ===\n";