-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comment.class.php
67 lines (57 loc) · 1.78 KB
/
Comment.class.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
<?php
// The Comment class is another class building upon the general Model class for most
// of it's database handling. The exception is deleting a comment.
class Comment extends Model {
public $user_id;
public $post_id;
public $posted_at;
public $title;
public $comment;
// Constructor
public function __construct() {}
// Create a comment object from a comment form POST request
public function create($commentRequest) {
$new = new Comment;
$new->user_id = $_SESSION['id'];
$new->posted_at = date("Y-m-d H:i:s");
$new->post_id = $commentRequest['post_id'];
$new->title = $commentRequest['title'];
$new->comment = $commentRequest['comment'];
$new->save();
}
// Save a Comment instance to the database
public function save(){
Model::save($this);
}
// Return the Post object this Comment belongs to
public function post() {
return Post::find($this->post_id);
}
// Return the user that wrote this Comment
public function user() {
return User::find($this->user_id);
}
// Return a Comment object by given id
public function find($id) {
if (func_num_args() != 1) exit ("Wrong number of arguments in Comment::find()");
return Model::find("Comment", $id);
}
// Return all Comments by given conditions
public function getALL($conditions=""){
if (func_num_args() > 1) exit ("Wrong number of arguments in Comment::getAll()");
return Model::getAll("Comment", $conditions);
}
// Delete a comment from the database by a given id
public function delete($commentId){
global $database;
try{
$query = "DELETE FROM Comment WHERE id=?";
$stmt = $database->prepare($query);
$stmt->execute(array($commentId));
}catch (Exception $e) {
echo 'Error: '.$e;
return false;
}
}
}
?>