-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathBlog.php
More file actions
71 lines (61 loc) · 2.24 KB
/
Blog.php
File metadata and controls
71 lines (61 loc) · 2.24 KB
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
<?php
/**
* @author Pierre-Henry Soria <phy@hizup.uk>
* @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved.
* @license Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
* @link http://hizup.uk
*/
namespace TestProject\Model;
class Blog
{
protected $oDb;
private $oCommentModel;
public function __construct()
{
$this->oDb = new \TestProject\Engine\Db;
$this->oCommentModel = new \TestProject\Model\Comment;
}
public function get($iOffset, $iLimit)
{
$oStmt = $this->oDb->prepare('SELECT * FROM Posts ORDER BY createdDate DESC LIMIT :offset, :limit');
$oStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
$oStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
$oStmt->execute();
return $oStmt->fetchAll(\PDO::FETCH_OBJ);
}
public function getAll()
{
$oStmt = $this->oDb->query('SELECT * FROM Posts ORDER BY createdDate DESC');
return $oStmt->fetchAll(\PDO::FETCH_OBJ);
}
public function getPostComments($iPostId)
{
return $this->oCommentModel->getPostComments($iPostId);
}
public function add(array $aData)
{
$oStmt = $this->oDb->prepare('INSERT INTO Posts (title, body, createdDate) VALUES(:title, :body, :created_date)');
return $oStmt->execute($aData);
}
public function getById($iId)
{
$oStmt = $this->oDb->prepare('SELECT * FROM Posts WHERE id = :postId LIMIT 1');
$oStmt->bindParam(':postId', $iId, \PDO::PARAM_INT);
$oStmt->execute();
return $oStmt->fetch(\PDO::FETCH_OBJ);
}
public function update(array $aData)
{
$oStmt = $this->oDb->prepare('UPDATE Posts SET title = :title, body = :body WHERE id = :postId LIMIT 1');
$oStmt->bindValue(':postId', $aData['post_id'], \PDO::PARAM_INT);
$oStmt->bindValue(':title', $aData['title']);
$oStmt->bindValue(':body', $aData['body']);
return $oStmt->execute();
}
public function delete($iId)
{
$oStmt = $this->oDb->prepare('DELETE FROM Posts WHERE id = :postId LIMIT 1');
$oStmt->bindParam(':postId', $iId, \PDO::PARAM_INT);
return $oStmt->execute();
}
}