Files
api/objects/departments_o.php
T
2024-12-16 08:54:03 +01:00

73 lines
2.0 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class departments_o extends db
{
use db_object_t;
public object_property $name;
public object_property $description;
public function structure(): void
{
$this->setTable('departments');
}
public function getObjectProperties(): void
{
$this->name = new object_property($this->table, $this->id, 'name', 'string', true);
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
}
public function create(string $name, string $description): void
{
global $db;
// Avoid SQL injection
$name = $db->escape_string($name);
$description = $db->escape_string($description);
// Create a new record in the database
$sql = "INSERT INTO $this->table (name, description) VALUES ('$name', '$description')";
$db->query($sql);
// Get the id of the new record
$this->id = $db->insert_id();
// Set the values of the object properties
$this->getObjectProperties();
}
public function edit(int $id, string $name, string $description): void
{
global $db;
$this->id = $id;
// Avoid SQL injection
$name = $db->escape_string($name);
$description = $db->escape_string($description);
// Update the record in the database
$sql = "UPDATE $this->table SET name = '$name', description = '$description' WHERE id = $this->id";
$db->query($sql);
// Set the values of the object properties
$this->getObjectProperties();
}
public function list(): array
{
global $db;
$sql = "SELECT * FROM $this->table";
$result = $db->query($sql);
return $db->fetch_all($result);
}
public function getDepartmentById(int $id): array
{
global $db;
$sql = "SELECT * FROM $this->table WHERE id = $id";
$result = $db->query($sql);
return $db->fetch_assoc($result);
}
}