-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.php
120 lines (112 loc) · 2.3 KB
/
posts.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html>
<head>
<title>Posts</title>
</head>
<body>
<h3>All</h3>
<?php
require('includes/config.php');
require('includes/connection.php');
require('classes/Post.php');
$posts = Post::all();
foreach ($posts as $post) {
echo json_encode(array(
'id' => $post->id,
'user_id' => $post->user_id,
'title' => $post->title,
'body' => $post->body,
'created_at' => $post->created_at,
'updated_at' => $post->updated_at,
));
echo '<br>';
}
?>
<h3>Find</h3>
<?php
$post = Post::find(1);
echo json_encode(array(
'id' => $post->id,
'user_id' => $post->user_id,
'title' => $post->title,
'body' => $post->body,
'created_at' => $post->created_at,
'updated_at' => $post->updated_at,
));
?>
<h3>Save</h3>
<?php
$post = new Post([
'title' => 'Foobar',
'body' => 'Lorem ipsum',
'user_id' => 1
]);
$post->save();
?>
<h3>Update</h3>
<?php
$post = Post::find(8);
echo json_encode(array(
'before' => array(
'id' => $post->id,
'user_id' => $post->user_id,
'title' => $post->title,
'body' => $post->body,
'created_at' => $post->created_at,
'updated_at' => $post->updated_at,
)
));
echo '<br>';
$post->title = 'Flurp';
$post->body = 'Flurparooni';
$post->user_id = 1;
echo json_encode(array(
'after' => array(
'id' => $post->id,
'user_id' => $post->user_id,
'title' => $post->title,
'body' => $post->body,
'created_at' => $post->created_at,
'updated_at' => $post->updated_at,
)
));
echo '<br>';
$post->update();
?>
<h3>Delete</h3>
<?php
$post = Post::find(87);
$post->delete();
?>
<h3>User</h3>
<?php
$post = Post::find(2);
$user = $post->user();
echo json_encode(array(
$user->id => array(
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'created_at' => $user->created_at,
)
));
?>
<h3>Comments</h3>
<?php
$post = Post::find(1);
$comments = $post->comments(1);
foreach ($comments as $comment) {
echo json_encode(array(
$comment->id => array(
'user_id' => $comment->user_id,
'post_id' => $comment->post_id,
'body' => $comment->body,
'created_at' => $comment->created_at,
'updated_at' => $comment->updated_at,
)
));
echo '<br>';
}
?>
</body>
</html>