DescriptionModel.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Models;
  3. class DescriptionModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. $dbFile = $_ENV['DB_FILE'];
  9. $dbPath = __DIR__ . '/../' . $dbFile;
  10. $this->pdo = new \PDO("sqlite:" . $dbPath);
  11. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  12. }
  13. public function getDescriptionsByCompany(int $companyId): array
  14. {
  15. $stmt = $this->pdo->prepare("
  16. SELECT d.description_id, d.description_text, d.product_id
  17. FROM description d
  18. WHERE d.company_id = :company_id
  19. ");
  20. $stmt->execute(['company_id' => $companyId]);
  21. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  22. }
  23. public function addDescription(int $productId, int $companyId, string $text): bool
  24. {
  25. $stmt = $this->pdo->prepare("
  26. INSERT INTO description (description_text, product_id, company_id)
  27. VALUES (:text, :product_id, :company_id)
  28. ");
  29. return $stmt->execute([
  30. 'text' => $text,
  31. 'product_id' => $productId,
  32. 'company_id' => $companyId,
  33. ]);
  34. }
  35. public function updateDescription(int $descriptionId, string $text): bool
  36. {
  37. $stmt = $this->pdo->prepare("
  38. UPDATE description SET description_text = :text
  39. WHERE description_id = :id
  40. ");
  41. return $stmt->execute([
  42. 'text' => $text,
  43. 'id' => $descriptionId,
  44. ]);
  45. }
  46. }