43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
|
|
use Core\Model;
|
|
|
|
class Setting extends Model
|
|
{
|
|
protected $table = 'settings';
|
|
protected $orderBy = 'id';
|
|
|
|
/** 读取单个设置 */
|
|
public function get(string $key, $default = '')
|
|
{
|
|
$row = $this->where('skey', $key);
|
|
return $row ? $row['sval'] : $default;
|
|
}
|
|
|
|
/** 批量读取为 [skey => sval] */
|
|
public function allKV(): array
|
|
{
|
|
$out = [];
|
|
foreach ($this->all() as $r) $out[$r['skey']] = $r['sval'];
|
|
return $out;
|
|
}
|
|
|
|
/** 设置(不存在则新增) */
|
|
public function set(string $key, $val, string $group = 'site'): void
|
|
{
|
|
$row = $this->where('skey', $key);
|
|
if ($row) {
|
|
$this->update($row['id'], ['sval' => $val, 'sgroup' => $group]);
|
|
} else {
|
|
$this->insert(['skey' => $key, 'sval' => $val, 'sgroup' => $group]);
|
|
}
|
|
}
|
|
|
|
/** 批量保存 */
|
|
public function saveMany(array $pairs, string $group = 'site'): void
|
|
{
|
|
foreach ($pairs as $k => $v) $this->set($k, $v, $group);
|
|
}
|
|
}
|