forked from noetix/Simple-ORM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
65 lines (49 loc) · 1.51 KB
/
example.php
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
<?php
// Load parameters.
$params = parse_ini_file(sprintf('%s/parameters.ini', __DIR__), true);
// Include the SimpleOrm class
include 'SimpleOrm.class.php';
// Connect to the database using mysqli
$conn = new mysqli($params['database']['host'], $params['database']['user'], $params['database']['password']);
if ($conn->connect_error)
die(sprintf('Unable to connect to the database. %s', $conn->connect_error));
// Tell SimpleOrm to use the connection you just created.
SimpleOrm::useConnection($conn, $params['database']['name']);
// Define an object that relates to a table.
class Blog extends SimpleOrm { }
// Create an entry.
$entry = new Blog;
$entry->title = 'Hello';
$entry->body = 'World!';
$entry->save();
// Use the object.
printf("%s\n", $entry->title); // prints 'Hello';
// Dump all the fields in the object.
print_r($entry->get());
// Retrieve a record from the table.
$entry = Blog::retrieveByPK($entry->id()); // by primary key
// Retrieve a record from the table using another column.
$entry = Blog::retrieveByTitle('Hello', SimpleOrm::FETCH_ONE); // by field (subject = hello)
// Update the object.
$entry->body = 'Mars!';
$entry->save();
// Delete the record from the table.
$entry->delete();
/*
vm1:/home/alex.joyce/SimpleOrm# php example.php
Hello
Array
(
[id] => 1
[title] => Hello
[body] => World!
)
vm1:/home/alex.joyce/SimpleOrm# php example.php
Hello
Array
(
[id] => 2
[title] => Hello
[body] => World!
)
*/