Update Config model

The first letter of the value from the conf_value field indicates the data type.
I -> integer, a -> array, other -> string.
This commit is contained in:
Visman 2020-09-20 22:45:19 +07:00
parent 921ede0968
commit bf5e4df5b0
2 changed files with 34 additions and 3 deletions

View file

@ -14,10 +14,27 @@ class Load extends Method
*/
public function load(): Config
{
$query = 'SELECT cf.conf_name, cf.conf_value
$config = [];
$query = 'SELECT cf.conf_name, cf.conf_value
FROM ::config AS cf';
$config = $this->c->DB->query($query)->fetchAll(PDO::FETCH_KEY_PAIR);
$stmt = $this->c->DB->query($query);
while ($row = $stmt->fetch()) {
switch ($row['conf_name'][0]) {
case 'a':
$value = \json_decode($row['conf_value'], true, 512, \JSON_THROW_ON_ERROR);
break;
case 'i':
$value = (int) $row['conf_value'];
break;
default:
$value = $row['conf_value'];
break;
}
$config[$row['conf_name']] = $value;
}
$this->model->setAttrs($config);
$this->c->Cache->set('config', $config);

View file

@ -21,8 +21,22 @@ class Save extends Method
$values = $this->model->getAttrs();
foreach ($modified as $name) {
if (\array_key_exists($name, $values)) {
switch ($name[0]) {
case 'a':
$value = \json_encode($values[$name], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_THROW_ON_ERROR);
break;
case 'i':
if (null !== $values[$name]) {
$value = (string) $values[$name];
break;
}
default:
$value = $values[$name];
break;
}
$vars = [
':value' => $values[$name],
':value' => $value,
':name' => $name
];
//????