- 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:
134
Core/Model.php
134
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);
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user