|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
require 'vendor/autoload.php'; |
|
3
|
|
|
|
|
4
|
|
|
use Ganga\Potato\Potato; |
|
5
|
|
|
use Ganga\Potato\Connection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* NOTE:: Create a test.db database fil |
|
9
|
|
|
* and run the users.sql dump file in the root of the project |
|
10
|
|
|
* to create the users table and some records |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* It is advisable that classes are created on their own file |
|
15
|
|
|
* This class definition will connect to the users table |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
class User extends Potato |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
// SQLite config example |
|
24
|
|
|
// Make sure a users table exists |
|
25
|
|
|
// A sample users.sql dump is provided in the root of the project |
|
26
|
|
|
$sqliteConfig = [ |
|
27
|
|
|
"type" => "sqlite", |
|
28
|
|
|
"database" => "test.db" |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
// Load environment variables |
|
32
|
|
|
$dotenv = new Dotenv\Dotenv(__DIR__); |
|
33
|
|
|
$dotenv->load(); |
|
34
|
|
|
|
|
35
|
|
|
// MySQL config example |
|
36
|
|
|
// Make sure you have a mysql database connection available |
|
37
|
|
|
// And that the connected database has a users table with id, name, address, company and age |
|
38
|
|
|
// As culumns |
|
39
|
|
|
// Fill in the fields below with the relevant ones |
|
40
|
|
|
$mysqlConfig = [ |
|
41
|
|
|
"type" => getenv('DB_TYPE'), |
|
42
|
|
|
"database" => getenv('DB_NAME'), |
|
43
|
|
|
"user" => getenv('DB_USER'), |
|
44
|
|
|
"password" => getenv('DB_PASS'), |
|
45
|
|
|
"host" => getenv('DB_HOST') |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
// Make sure you choose the correct config type |
|
49
|
|
|
$conn = new Connection($mysqlConfig); |
|
50
|
|
|
|
|
51
|
|
|
// Get all users from the users table |
|
52
|
|
|
$users = User::getAll(); |
|
53
|
|
|
echo '<h3>All Users</h3>'; |
|
54
|
|
|
prettyPrint($users); |
|
55
|
|
|
|
|
56
|
|
|
// Get one user from the users table |
|
57
|
|
|
$user = User::getOne(3);// |
|
58
|
|
|
echo '<h3>One User</h3>'; |
|
59
|
|
|
prettyPrint($user); |
|
60
|
|
|
|
|
61
|
|
|
// Create a new user |
|
62
|
|
|
$user = new User; |
|
63
|
|
|
$user->name = "Jin Kazama"; |
|
|
|
|
|
|
64
|
|
|
$user->age = 21; |
|
|
|
|
|
|
65
|
|
|
$user->address = "525 Kindaruma"; |
|
|
|
|
|
|
66
|
|
|
$user->company = "466 Video Productions"; |
|
|
|
|
|
|
67
|
|
|
$user->save(); |
|
68
|
|
|
$users = User::getAll(); |
|
69
|
|
|
echo '<h3>All Users</h3>'; |
|
70
|
|
|
prettyPrint($users); |
|
71
|
|
|
|
|
72
|
|
|
// Edit an existing user |
|
73
|
|
|
$user = User::find(1000); |
|
74
|
|
|
$user->name = "Anthony Steven"; |
|
|
|
|
|
|
75
|
|
|
$user->save(); |
|
76
|
|
|
$users = User::getAll(); |
|
77
|
|
|
echo '<h3>All Users (3rd edited)</h3>'; |
|
78
|
|
|
prettyPrint($users); |
|
79
|
|
|
|
|
80
|
|
|
// Delete a user |
|
81
|
|
|
User::destroy(2); |
|
|
|
|
|
|
82
|
|
|
$user_del = User::getAll(); |
|
83
|
|
|
echo '<h3>All Users (id 2 deleted)</h3>'; |
|
84
|
|
|
prettyPrint($user_del); |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
function prettyPrint($data) |
|
88
|
|
|
{ |
|
89
|
|
|
echo '<pre>'.print_r($data, true); |
|
90
|
|
|
} |
|
91
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.