77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use traits\db_object_t;
|
|
|
|
class user_key_value_pairs_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public int $user_id;
|
|
public object_property $var;
|
|
public object_property $val;
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('user_key_value_pairs');
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->var = new object_property($this->table, $this->id, 'var', 'string', true);
|
|
$this->val = new object_property($this->table, $this->id, 'val', 'mixed', true);
|
|
}
|
|
|
|
public function setUser($user_id): user_key_value_pairs_o
|
|
{
|
|
$this->user_id = $user_id;
|
|
return $this;
|
|
}
|
|
|
|
public function setValue($var, $val): user_key_value_pairs_o
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$var = $db->escape_string($var);
|
|
$val = $db->escape_string($val);
|
|
// Check if the value exists in the database
|
|
$exists = $this->getValue($var);
|
|
// Create a new record in the database if it doesn't exist
|
|
if ($exists !== null) {
|
|
$sql = "UPDATE $this->table SET val = '$val' WHERE user_id = $this->user_id AND var = '$var'";
|
|
} else {
|
|
$sql = "INSERT INTO $this->table (user_id, var, val) VALUES ($this->user_id, '$var', '$val')";
|
|
}
|
|
$db->query($sql);
|
|
return $this;
|
|
}
|
|
|
|
public function getValue($var): mixed
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$var = $db->escape_string($var);
|
|
// Get the record from the database
|
|
$sql = "SELECT val FROM $this->table WHERE user_id = $this->user_id AND var = '$var'";
|
|
$result = $db->query($sql);
|
|
if ($result->num_rows > 0) {
|
|
return $db->fetch_assoc($result)['val'];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function deleteValue($var): user_key_value_pairs_o
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$var = $db->escape_string($var);
|
|
// Create a new record in the database
|
|
$sql = "DELETE FROM $this->table WHERE user_id = $this->user_id AND var = '$var'";
|
|
$db->query($sql);
|
|
return $this;
|
|
}
|
|
|
|
} |