58 lines
2.0 KiB
PHP
58 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace classes;
|
|
|
|
class object_property
|
|
{
|
|
private string $table; // The table of the objects in the database (e.g. users)
|
|
public int $id; // The id of the object in the database
|
|
private string $column; // The column name of the field in the database table (e.g. id, name, email)
|
|
private string $type; // The data type of the field in the database table (e.g. int, varchar, text)
|
|
private bool $required; // Whether the field is required or not
|
|
private mixed $default; // The default value of the field
|
|
|
|
public function __construct(string $table, int $id, string $column, string $type, bool $required = false, mixed $default = null)
|
|
{
|
|
$this->table = $table;
|
|
$this->id = $id;
|
|
$this->column = $column;
|
|
$this->type = $type;
|
|
$this->required = $required;
|
|
$this->default = $default;
|
|
}
|
|
|
|
/**
|
|
* Get the value of the field in the database table
|
|
* @return mixed The value of the field in the database table
|
|
*/
|
|
public function value(): mixed
|
|
{
|
|
// Get the value of the field in the database table
|
|
global $db;
|
|
$sql = "SELECT $this->column FROM $this->table WHERE id = $this->id";
|
|
$result = $db->query($sql);
|
|
$row = $db->fetch_assoc($result);
|
|
return $row[$this->column];
|
|
}
|
|
|
|
/**
|
|
* Set the value of the field in the database table
|
|
* @param mixed $value The value of the field in the database table
|
|
*/
|
|
public function set(mixed $value): void
|
|
{
|
|
// Set the value of the field in the database table
|
|
global /** @var db $db */
|
|
$db;
|
|
// If the value is null, set it to null
|
|
if ($value === null) {
|
|
$sql = "UPDATE $this->table SET $this->column = NULL WHERE id = $this->id";
|
|
}
|
|
// If the value is a string, escape it
|
|
else {
|
|
$value = $db->escape_string($value);
|
|
$sql = "UPDATE $this->table SET $this->column = '$value' WHERE id = $this->id";
|
|
}
|
|
$db->query($sql);
|
|
}
|
|
} |