mirror of
https://github.com/mivodev/mivo.git
synced 2026-01-26 13:31:56 +07:00
Initial Release v1.0.0: Full feature set with Docker automation, Nginx/Alpine stack
This commit is contained in:
46
app/Models/Setting.php
Normal file
46
app/Models/Setting.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
class Setting {
|
||||
private $db;
|
||||
private $table = 'settings';
|
||||
|
||||
public function __construct() {
|
||||
$this->db = Database::getInstance();
|
||||
$this->initTable();
|
||||
}
|
||||
|
||||
private function initTable() {
|
||||
$sql = "CREATE TABLE IF NOT EXISTS {$this->table} (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
)";
|
||||
$this->db->query($sql);
|
||||
}
|
||||
|
||||
public function get($key, $default = null) {
|
||||
$stmt = $this->db->query("SELECT value FROM {$this->table} WHERE key = ?", [$key]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ? $row['value'] : $default;
|
||||
}
|
||||
|
||||
public function set($key, $value) {
|
||||
// SQLite Upsert
|
||||
$sql = "INSERT INTO {$this->table} (key, value) VALUES (:key, :value)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value";
|
||||
return $this->db->query($sql, ['key' => $key, 'value' => $value]);
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
$stmt = $this->db->query("SELECT * FROM {$this->table}");
|
||||
$results = $stmt->fetchAll();
|
||||
$settings = [];
|
||||
foreach ($results as $row) {
|
||||
$settings[$row['key']] = $row['value'];
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user