lepisi-pengumuman/Core/Model.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

185 lines
4.8 KiB
PHP

<?php
namespace Core;
use App\Config;
abstract class Model
{
protected static $conn = null;
abstract public function showAll();
abstract public function showSingle($id);
protected static function connectDB()
{
try {
if (!self::$conn) {
$dsn = 'mysql:host='.Config::DB_HOST.';dbname='.Config::DB_DB;
self::$conn = new \PDO($dsn, Config::DB_UNAME, Config::DB_PWD);
self::$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
return self::$conn;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function createTable($table, $fields = [])
{
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$db = static::connectDB();
$query = $db->prepare($sql);
if ($query->execute()) {
return true;
}
return false;
}
public function dropTable($table)
{
if (is_array($table)) {
if (count($table)) {
$table = implode(', ', $table);
}
}
$sql = "DROP TABLE IF EXISTS {$table}";
$db = static::connectDB();
$query = $db->prepare($sql);
if ($query->execute()) {
return true;
}
return false;
}
public function entry($table, $args, $values = '')
{
if (count($args)) {
$keys = '`'.implode('`, `', array_keys($args)).'`';
// This is if want to insert multiple rows
foreach ($args as $key => $val) {
if (preg_match('/,/', $val)) {
$val = explode(',', $val);
$args[$key] = $val;
}
}
$x = 1;
foreach ($args as $field) {
// Setting the query for multiple rows
if (is_array($field)) {
foreach ($field as $fields) {
$values .= '(?)';
if ($x < count($field)) {
$values .= ', ';
}
$x++;
}
} else {
if ($x === 1) {
$values .= '(';
}
$values .= '?';
if ($x < count($args)) {
$values .= ', ';
} else {
$values .= ')';
}
$x++;
}
}
try {
$sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}";
$db = static::connectDB();
$query = $db->prepare($sql);
$x = 1;
foreach ($args as $value) {
if (is_array($value)) {
foreach ($value as $vals) {
$query->bindValue($x, $vals);
$x++;
}
} else {
$query->bindValue($x, $value);
$x++;
}
}
if ($query->execute()) {
return true;
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
public function update($table, $args, $id)
{
if (count($args)) {
$keys = array_keys($args);
$fields = [];
foreach ($keys as $key) {
$fields[] = $key.' = ?';
}
if (count($fields) > 1) {
$fields = implode(', ', $fields);
} else {
$fields = implode('', $fields);
}
try {
$db = static::connectDB();
$sql = "UPDATE {$table} SET {$fields} WHERE id = ?";
$query = $db->prepare($sql);
$x = 1;
foreach ($args as $value) {
$query->bindValue($x, $value);
$x++;
}
$query->bindValue($x, $id);
if ($query->execute()) {
return true;
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
public function delete($table, $id)
{
try {
$db = static::connectDB();
$sql = "DELETE FROM {$table} WHERE id = ?";
$query = $db->prepare($sql);
if ($query->execute([$id])) {
return true;
}
return false;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}