|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use idiorm\orm\ORM; |
|
4
|
|
|
|
|
5
|
|
|
// ------------------- // |
|
6
|
|
|
// --- Idiorm Demo --- // |
|
7
|
|
|
// ------------------- // |
|
8
|
|
|
|
|
9
|
|
|
// Note: This is just about the simplest database-driven webapp it's possible to create |
|
10
|
|
|
// and is designed only for the purpose of demonstrating how Idiorm works. |
|
11
|
|
|
|
|
12
|
|
|
// In case it's not obvious: this is not the correct way to build web applications! |
|
13
|
|
|
|
|
14
|
|
|
// Require the idiorm file |
|
15
|
|
|
|
|
16
|
|
|
require_once '../vendor/autoload.php'; |
|
17
|
|
|
|
|
18
|
|
|
// Connect to the demo database file |
|
19
|
|
|
ORM::configure('sqlite:./demo.sqlite'); |
|
20
|
|
|
|
|
21
|
|
|
// This grabs the raw database connection from the ORM |
|
22
|
|
|
// class and creates the table if it doesn't already exist. |
|
23
|
|
|
// Wouldn't normally be needed if the table is already there. |
|
24
|
|
|
$db = ORM::get_db(); |
|
25
|
|
|
$sql = ' |
|
26
|
|
|
CREATE TABLE IF NOT EXISTS contact ( |
|
27
|
|
|
id INTEGER PRIMARY KEY, |
|
28
|
|
|
name TEXT, |
|
29
|
|
|
email TEXT |
|
30
|
|
|
); |
|
31
|
|
|
'; |
|
32
|
|
|
$db->exec($sql); |
|
33
|
|
|
|
|
34
|
|
|
// Handle POST submission |
|
35
|
|
|
if (isset($_POST)) { |
|
36
|
|
|
|
|
37
|
|
|
// Create a new contact object |
|
38
|
|
|
$contact = ORM::for_table('contact')->create(); |
|
39
|
|
|
|
|
40
|
|
|
// SHOULD BE MORE ERROR CHECKING HERE! |
|
41
|
|
|
|
|
42
|
|
|
// Set the properties of the object |
|
43
|
|
|
$contact->name = $_POST['name']; |
|
|
|
|
|
|
44
|
|
|
$contact->email = $_POST['email']; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
// Save the object to the database |
|
47
|
|
|
$contact->save(); |
|
48
|
|
|
|
|
49
|
|
|
// Redirect to self. |
|
50
|
|
|
header('Location: ' . basename(__FILE__)); |
|
51
|
|
|
exit; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Get a list of all contacts from the database |
|
55
|
|
|
$count = ORM::for_table('contact')->count(); |
|
56
|
|
|
$contact_list = ORM::for_table('contact')->find_many(); |
|
57
|
|
|
?> |
|
58
|
|
|
|
|
59
|
|
|
<html> |
|
60
|
|
|
<head> |
|
61
|
|
|
<title>Idiorm Demo</title> |
|
62
|
|
|
</head> |
|
63
|
|
|
|
|
64
|
|
|
<body> |
|
65
|
|
|
|
|
66
|
|
|
<h1>Idiorm Demo</h1> |
|
67
|
|
|
|
|
68
|
|
|
<h2>Contact List (<?php echo $count; ?> contacts)</h2> |
|
69
|
|
|
<ul> |
|
70
|
|
|
<?php foreach ($contact_list as $contact): ?> |
|
71
|
|
|
<li> |
|
72
|
|
|
<strong><?php echo $contact->name ?></strong> |
|
73
|
|
|
<a href="mailto:<?php echo $contact->email; ?>"><?php echo $contact->email; ?></a> |
|
74
|
|
|
</li> |
|
75
|
|
|
<?php endforeach; ?> |
|
76
|
|
|
</ul> |
|
77
|
|
|
|
|
78
|
|
|
<form method="post" action=""> |
|
79
|
|
|
<h2>Add Contact</h2> |
|
80
|
|
|
<p><label for="name">Name:</label> <input type="text" id="name" name="name" /></p> |
|
81
|
|
|
<p><label for="email">Email:</label> <input type="email" id="email" name="email" /></p> |
|
82
|
|
|
<input type="submit" value="Create" /> |
|
83
|
|
|
</form> |
|
84
|
|
|
</body> |
|
85
|
|
|
</html> |
|
86
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.