setTable('customer_vehicles'); } public function getObjectProperties(): void { $this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true); $this->type = new object_property($this->table, $this->id, 'type', 'string', true); $this->reg = new object_property($this->table, $this->id, 'reg', 'string', true); $this->notes = new object_property($this->table, $this->id, 'notes', 'string', true); $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false); } public function add(int $customer_id, string $type, string $reg, string $notes): customer_vehicles_o { global $db, $response; try { // Avoid SQL injection $type = $db->escape_string($type); $reg = $db->escape_string($reg); $notes = $db->escape_string($notes); // Create a new record in the database $sql = "INSERT INTO $this->table (customer_id, type, reg, notes) VALUES ($customer_id, '$type', '$reg', '$notes')"; $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()); } return $this; } public function getCustomerVehiclesAsArray(int $customer_id): array { global $db; $sql = "SELECT * FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL"; $result = $db->query($sql); $customer_notes = []; if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $customer_notes[] = $row; } } return $customer_notes; } public function getCustomerVehiclesPaginated($customer_id, $page = 1 , $limit = 10): array { global /** @var response $response */ $db, $response; $sql = "SELECT * FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL LIMIT $limit OFFSET " . ($page - 1) * $limit; $result = $db->query($sql); $array = $db->fetch_all($result); $sql = "SELECT COUNT(*) as count FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL"; $result = $db->query($sql); $row = $db->fetch_assoc($result); $total = $row['count']; $response->paginate($page, $limit, $total); return $array; } public function getCustomerVehicleById(int $id): customer_vehicles_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 delete(int $id): void { global $db; $this->id = $id; $sql = "UPDATE $this->table SET deleted_at = NOW() WHERE id = $this->id"; $db->query($sql); } public function restore(int $id): void { global $db; $this->id = $id; $sql = "UPDATE $this->table SET deleted_at = NULL WHERE id = $this->id"; $db->query($sql); } public function getArrayByObjectProperties(): array { return [ 'id' => $this->id, 'customer_id' => $this->customer_id->value(), 'type' => $this->type->value(), 'reg' => $this->reg->value(), 'notes' => $this->notes->value(), 'deleted_at' => $this->deleted_at->value() ]; } }