lepisi-pengumuman/App/Models/Post.php
Gregorio Chiko Putra 62430ff905 - Methods that are same (e.g. entry(), update(), delete(), etc.) defined in the parent.
- Added function when entry multiple rows (in this case is when putting
categories).
- Do test.
2017-09-04 10:37:25 +07:00

75 lines
1.9 KiB
PHP

<?php
namespace App\Models;
class Post extends \Core\Model
{
public function __construct()
{
// Create table for posts
$this->createTable(
'pengumuman',
[
'id int(3) NOT NULL AUTO_INCREMENT',
'category int(3) NOT NULL',
'created_at timestamp DEFAULT CURRENT_TIMESTAMP',
'expired_at timestamp NOT NULL',
'creator int(3) NOT NULL',
'edited_at timestamp',
'editor timestamp',
'content varchar(255) NOT NULL',
'PRIMARY KEY (id)'
]
);
// Create table for categories
$this->createTable(
'kategori',
[
'id int(3) NOT NULL AUTO_INCREMENT',
'category varchar(20) NOT NULL',
'PRIMARY KEY (id)'
]
);
}
public function showAll()
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman ORDER BY created_at";
if ($stmt = $db->query($sql)) {
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
// For tests
return true;
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function showSingle($id)
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman WHERE id = ?";
$query = $db->prepare($sql);
if ($query->execute([$id])) {
if ($query->rowCount() === 1) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
// For tests
return true;
}
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}