- 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:
Gregorio Chiko Putra 2017-09-04 10:37:25 +07:00
parent 58264e39db
commit 62430ff905
5 changed files with 165 additions and 198 deletions

View File

@ -54,93 +54,4 @@ class Access extends \Core\Model
echo $e->getMessage(); 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();
}
}
} }

View File

@ -5,6 +5,7 @@ class Post extends \Core\Model
{ {
public function __construct() public function __construct()
{ {
// Create table for posts
$this->createTable( $this->createTable(
'pengumuman', 'pengumuman',
[ [
@ -19,6 +20,16 @@ class Post extends \Core\Model
'PRIMARY KEY (id)' '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() public function showAll()
@ -60,95 +71,4 @@ class Post extends \Core\Model
echo $e->getMessage(); 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();
}
}
} }

View File

@ -7,6 +7,10 @@ abstract class Model
{ {
protected static $conn = null; protected static $conn = null;
abstract public function showAll();
abstract public function showSingle($id);
protected static function connectDB() protected static function connectDB()
{ {
try { try {
@ -37,6 +41,11 @@ abstract class Model
public function dropTable($table) public function dropTable($table)
{ {
if (is_array($table)) {
if (count($table)) {
$table = implode(', ', $table);
}
}
$sql = "DROP TABLE IF EXISTS {$table}"; $sql = "DROP TABLE IF EXISTS {$table}";
$db = static::connectDB(); $db = static::connectDB();
@ -48,13 +57,128 @@ abstract class Model
return false; 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); // This is if want to insert multiple rows
foreach ($args as $key => $val) {
if (preg_match('/,/', $val)) {
$val = explode(',', $val);
$args[$key] = $val;
}
}
abstract public function entry($args); $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++;
}
}
abstract public function update($args, $id); try {
$sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}";
abstract public function delete($id); $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();
}
}
} }

View File

@ -13,23 +13,23 @@ class AccessTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($access->showAll()); $this->assertTrue($access->showAll());
$args = [ $user_entry = [
'username' => 'gregorio', 'username' => 'gregorio',
'password' => 'iniada13charc', 'password' => 'iniada13charc',
]; ];
$this->assertTrue($access->entry($args)); $this->assertTrue($access->entry('user', $user_entry));
$args = [ $user_update = [
'username' => 'masihgregorio', 'username' => 'masihgregorio',
'password' => 'inijuga3belas' 'password' => 'inijuga3belas'
]; ];
$this->assertTrue($access->update($args, 1)); $this->assertTrue($access->update('user', $user_update, 1));
$this->assertTrue($access->showSingle(1)); $this->assertTrue($access->showSingle(1));
$this->assertTrue($access->delete(1)); $this->assertTrue($access->delete('user', 1));
$this->assertTrue($access->dropTable('user')); $this->assertTrue($access->dropTable('user'));
} }

View File

@ -13,26 +13,38 @@ class PostTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($post->showAll()); $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, 'category' => 3,
'expired_at' => '2017-09-02', 'expired_at' => '2017-09-02',
'creator' => 2, 'creator' => 2,
'content' => 'barbarbarbarbarbarbar!' 'content' => 'barbarbarbarbarbarbar!'
]; ];
$this->assertTrue($post->entry($args)); $this->assertTrue($post->entry('pengumuman', $post_entry));
$args = [ $post_update = [
'category' => 5, 'category' => 2,
'content' => 'foofoofoofoo!' 'content' => 'foofoofoofoo!'
]; ];
$this->assertTrue($post->update($args, 1)); $this->assertTrue($post->update('pengumuman', $post_update, 1));
$this->assertTrue($post->showSingle(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']));
} }
} }