Added `delete`, `restore`, and other utility methods to enhance CRUD operations, including support for soft deletes. Introduced `objectChanged` hooks across objects for better cache or event handling, ensuring scalability and maintainability. Refactored and standardized object property handling while restructuring related methods.
76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace classes;
|
|
|
|
class object_property
|
|
{
|
|
public int $id; // The table of the objects in the database (e.g. users)
|
|
private string $table; // 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;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
// Return the value of the field in the database table
|
|
return $this->value();
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* Nullify the value of the field in the database table
|
|
*/
|
|
public function nullify(): void
|
|
{
|
|
// Set the value of the field in the database table to null
|
|
global /** @var db $db */
|
|
$db;
|
|
$sql = "UPDATE $this->table SET $this->column = NULL WHERE id = $this->id";
|
|
$db->query($sql);
|
|
}
|
|
} |