Modify Model set functions to abstract, create Access interactions, do test.
This commit is contained in:
146
App/Models/Access.php
Normal file
146
App/Models/Access.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
class Access extends \Core\Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->createTable(
|
||||
'user',
|
||||
[
|
||||
'id int(3) NOT NULL AUTO_INCREMENT',
|
||||
'username varchar(25) NOT NULL',
|
||||
'password char(13)',
|
||||
'registered_at timestamp DEFAULT CURRENT_TIMESTAMP',
|
||||
'PRIMARY KEY (id)'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function showAll()
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT id, username, registered_at FROM user";
|
||||
|
||||
if ($stmt = $db->query($sql)) {
|
||||
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function showSingle($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT id, username, registered_at FROM user WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
if ($query->rowCount() === 1) {
|
||||
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user