Modify Model become abstract, create database interactions and do test.

This commit is contained in:
2017-08-31 14:53:17 +07:00
parent 5ca179fac7
commit a075e1c36e
3 changed files with 190 additions and 17 deletions

View File

@@ -3,14 +3,14 @@ namespace Core;
use App\Config;
class Model
abstract 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;
@@ -24,20 +24,9 @@ class Model
}
}
public function createTable()
public function createTable($table, $fields = [])
{
$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;";
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$db = static::connectDB();
$query = $db->prepare($sql);
@@ -48,9 +37,9 @@ class Model
return false;
}
public function dropTable()
public function dropTable($table)
{
$sql = "DROP TABLE IF EXISTS pengumuman";
$sql = "DROP TABLE IF EXISTS {$table}";
$db = static::connectDB();
$query = $db->prepare($sql);