setTable('products'); } 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); $this->price = new object_property($this->table, $this->id, 'price', 'int', true); $this->category = new object_property($this->table, $this->id, 'category', 'string', true); $this->piktogram = new object_property($this->table, $this->id, 'piktogram', 'string', false); $this->economic_product_id = new object_property($this->table, $this->id, 'economic_product_id', 'int', false); } public function getProductById(int $id): products_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 add(string $name, string $description, int $price, string|bool $category = false, string|bool $piktogram = false, string|bool $economicProductId = false): void { global $db, $response; try { // Avoid SQL injection $name = $db->escape_string($name); $description = $db->escape_string($description); $category = $category ? $db->escape_string($category) : null; $piktogram = $piktogram ? $db->escape_string($piktogram) : ''; $economicProductId = $economicProductId ? $db->escape_string($economicProductId) : null; // Create a new record in the database $sql = "INSERT INTO $this->table (name, description, price, category, piktogram, economic_product_id) VALUES ('$name', '$description', $price, '$category', '$piktogram', '$economicProductId')"; $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 edit(mixed $id, mixed $name, mixed $description, mixed $price, mixed $category = false, mixed $piktogram = false, mixed $economicProductId = false): void { global $db, $response; $this->id = $id; try { // Avoid SQL injection $name = $db->escape_string($name); $description = $db->escape_string($description); $category = $category ? $db->escape_string($category) : null; $piktogram = $piktogram ? $db->escape_string($piktogram) : ''; $economicProductId = $economicProductId ? $db->escape_string($economicProductId) : null; // Update the record in the database $sql = "UPDATE $this->table SET name = '$name', description = '$description', price = $price, category = '$category', piktogram = '$piktogram', economic_product_id = '$economicProductId' WHERE id = $id"; $db->query($sql); // Set the values of the object properties $this->getObjectProperties(); } catch (\Exception $e) { $response->error($e->getMessage()); } } public function listObjectsByCategory(string $category): array { global $db; $category = $db->escape_string($category); $sql = "SELECT * FROM $this->table WHERE category = '$category'"; $result = $db->query($sql); return $db->fetch_all($result); } public function asArray(): array { return [ 'id' => (int)$this->id, 'name' => (string)$this->name->value(), 'description' => (string)$this->description->value(), 'price' => (int)$this->price->value(), 'category' => $this->category->value(), 'piktogram' => $this->piktogram->value(), 'economic_product_id' => $this->economic_product_id->value(), ]; } }