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.
90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use traits\db_object_t;
|
|
|
|
class plate_scans_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $plate_scanner_id;
|
|
public object_property $plate;
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('plate_scans');
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
// No need to invalidate the cache, since the plate_scans object is not cached
|
|
}
|
|
|
|
public function getPlateScanById(int $id): plate_scans_o
|
|
{
|
|
global $db;
|
|
// Get the record from the database
|
|
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
|
$result = $db->query($sql);
|
|
if ($result->num_rows > 0) {
|
|
$this->id = $id;
|
|
$this->getObjectProperties();
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->plate_scanner_id = new object_property($this->table, $this->id, 'plate_scanner_id', 'int');
|
|
$this->plate = new object_property($this->table, $this->id, 'plate', 'string');
|
|
}
|
|
|
|
public function add(int $plate_scanner_id, string $plate): void
|
|
{
|
|
global $db, $response;
|
|
try {
|
|
// Avoid SQL injection
|
|
$plate = $db->escape_string($plate);
|
|
// Get the department id from the plate scanner id
|
|
$sql = "SELECT department_id FROM plate_scanners WHERE id = $plate_scanner_id";
|
|
$result = $db->query($sql);
|
|
$row = $db->fetch_assoc($result);
|
|
$department_id = $row['department_id'];
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (plate_scanner_id, plate, department_id) VALUES ($plate_scanner_id, '$plate', $department_id)";
|
|
$db->query($sql);
|
|
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getPlateScansByDepartment(int $department_id, int $page, int $limit): array
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$department_id = $db->escape_string($department_id);
|
|
$page = $db->escape_string($page);
|
|
$limit = $db->escape_string($limit);
|
|
// Calculate the offset
|
|
$offset = ($page - 1) * $limit;
|
|
$sql = "SELECT * FROM $this->table WHERE department_id = $department_id ORDER BY id DESC LIMIT $limit OFFSET $offset";
|
|
$result = $db->query($sql);
|
|
$plate_scans = [];
|
|
if ($result->num_rows > 0) {
|
|
// Return the records as an array raw data
|
|
while ($row = $result->fetch_assoc()) {
|
|
$plate_scans[] = $row;
|
|
}
|
|
}
|
|
return $plate_scans;
|
|
}
|
|
} |