- 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.
This commit is contained in:
parent
58264e39db
commit
62430ff905
@ -54,93 +54,4 @@ class Access extends \Core\Model
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($args)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
|
||||
$x = 1;
|
||||
$values = '';
|
||||
foreach ($args as $field) {
|
||||
$values .= '?';
|
||||
if ($x < count($args)) {
|
||||
$values .= ', ';
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "INSERT INTO user ({$keys}) VALUES ({$values})";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($args, $id)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key.' = ?';
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE user SET ".implode(', ', $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($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "DELETE FROM user WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ class Post extends \Core\Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
// Create table for posts
|
||||
$this->createTable(
|
||||
'pengumuman',
|
||||
[
|
||||
@ -19,6 +20,16 @@ class Post extends \Core\Model
|
||||
'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()
|
||||
@ -60,95 +71,4 @@ class Post extends \Core\Model
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($args)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
|
||||
$x = 1;
|
||||
$values = '';
|
||||
foreach ($args as $field) {
|
||||
$values .= '?';
|
||||
if ($x < count($args)) {
|
||||
$values .= ', ';
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "INSERT INTO pengumuman ({$keys}) VALUES ({$values})";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$x = 1;
|
||||
|
||||
foreach ($args as $val) {
|
||||
$query->bindValue($x, urldecode($val));
|
||||
$x++;
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($args, $id)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key.' = ?';
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE pengumuman SET ".implode(', ', $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($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "DELETE FROM pengumuman WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
140
Core/Model.php
140
Core/Model.php
@ -7,6 +7,10 @@ abstract class Model
|
||||
{
|
||||
protected static $conn = null;
|
||||
|
||||
abstract public function showAll();
|
||||
|
||||
abstract public function showSingle($id);
|
||||
|
||||
protected static function connectDB()
|
||||
{
|
||||
try {
|
||||
@ -37,6 +41,11 @@ abstract class Model
|
||||
|
||||
public function dropTable($table)
|
||||
{
|
||||
if (is_array($table)) {
|
||||
if (count($table)) {
|
||||
$table = implode(', ', $table);
|
||||
}
|
||||
}
|
||||
$sql = "DROP TABLE IF EXISTS {$table}";
|
||||
|
||||
$db = static::connectDB();
|
||||
@ -48,13 +57,128 @@ abstract class Model
|
||||
return false;
|
||||
}
|
||||
|
||||
abstract public function showAll();
|
||||
public function entry($table, $args, $values = '')
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
|
||||
abstract public function showSingle($id);
|
||||
|
||||
abstract public function entry($args);
|
||||
|
||||
abstract public function update($args, $id);
|
||||
|
||||
abstract public function delete($id);
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,23 +13,23 @@ class AccessTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertTrue($access->showAll());
|
||||
|
||||
$args = [
|
||||
$user_entry = [
|
||||
'username' => 'gregorio',
|
||||
'password' => 'iniada13charc',
|
||||
];
|
||||
|
||||
$this->assertTrue($access->entry($args));
|
||||
$this->assertTrue($access->entry('user', $user_entry));
|
||||
|
||||
$args = [
|
||||
$user_update = [
|
||||
'username' => 'masihgregorio',
|
||||
'password' => 'inijuga3belas'
|
||||
];
|
||||
|
||||
$this->assertTrue($access->update($args, 1));
|
||||
$this->assertTrue($access->update('user', $user_update, 1));
|
||||
|
||||
$this->assertTrue($access->showSingle(1));
|
||||
|
||||
$this->assertTrue($access->delete(1));
|
||||
$this->assertTrue($access->delete('user', 1));
|
||||
|
||||
$this->assertTrue($access->dropTable('user'));
|
||||
}
|
||||
|
@ -13,26 +13,38 @@ class PostTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertTrue($post->showAll());
|
||||
|
||||
$args = [
|
||||
$category_entry = [
|
||||
'category' => 'Akademik,Jadwal,Dosen'
|
||||
];
|
||||
|
||||
$this->assertTrue($post->entry('kategori', $category_entry));
|
||||
|
||||
$category_update = [
|
||||
'category' => 'Akakademik'
|
||||
];
|
||||
|
||||
$this->assertTrue($post->update('kategori', $category_update, 1));
|
||||
|
||||
$post_entry = [
|
||||
'category' => 3,
|
||||
'expired_at' => '2017-09-02',
|
||||
'creator' => 2,
|
||||
'content' => 'barbarbarbarbarbarbar!'
|
||||
];
|
||||
|
||||
$this->assertTrue($post->entry($args));
|
||||
$this->assertTrue($post->entry('pengumuman', $post_entry));
|
||||
|
||||
$args = [
|
||||
'category' => 5,
|
||||
$post_update = [
|
||||
'category' => 2,
|
||||
'content' => 'foofoofoofoo!'
|
||||
];
|
||||
|
||||
$this->assertTrue($post->update($args, 1));
|
||||
$this->assertTrue($post->update('pengumuman', $post_update, 1));
|
||||
|
||||
$this->assertTrue($post->showSingle(1));
|
||||
|
||||
$this->assertTrue($post->delete(1));
|
||||
$this->assertTrue($post->delete('pengumuman', 1));
|
||||
|
||||
$this->assertTrue($post->dropTable('pengumuman'));
|
||||
$this->assertTrue($post->dropTable(['pengumuman', 'kategori']));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user