64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
namespace Core;
|
|
|
|
use App\Config;
|
|
|
|
class Model
|
|
{
|
|
protected static $dsn;
|
|
|
|
protected static function connectDB()
|
|
{
|
|
static $conn = null;
|
|
|
|
try {
|
|
if (!$conn) {
|
|
$dsn = 'mysql:host='.Config::DB_HOST.';dbname='.Config::DB_DB;
|
|
$conn = new \PDO($dsn, Config::DB_UNAME, Config::DB_PWD);
|
|
|
|
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
return $conn;
|
|
} catch (PDOException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function createTable()
|
|
{
|
|
$sql = "CREATE TABLE IF NOT EXISTS pengumuman (
|
|
id int(3) NOT NULL AUTO_INCREMENT,
|
|
category int(3) NOT NULL,
|
|
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
expire_at timestamp NOT NULL,
|
|
creator int(3) NOT NULL,
|
|
edited_at timestamp,
|
|
editor int(3),
|
|
content varchar(255) NOT NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
|
|
|
$db = static::connectDB();
|
|
$query = $db->prepare($sql);
|
|
|
|
if ($query->execute()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function dropTable()
|
|
{
|
|
$sql = "DROP TABLE IF EXISTS pengumuman";
|
|
|
|
$db = static::connectDB();
|
|
$query = $db->prepare($sql);
|
|
|
|
if ($query->execute()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|