CprPreregistrationModel.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Models;
  3. class CprPreregistrationModel
  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 insert(
  14. string $name,
  15. string $contactNumber,
  16. string $email,
  17. string $productType,
  18. string $internalControlNumber,
  19. int $productQuantity,
  20. string $propertyName,
  21. string $propertyLocation
  22. ): array {
  23. $stmt = $this->pdo->prepare(
  24. "INSERT INTO cpr_preregistration (
  25. name, contact_number, email, product_type, internal_control_number, product_quantity, property_name, property_location
  26. ) VALUES (
  27. :name, :contact_number, :email, :product_type, :internal_control_number, :product_quantity, :property_name, :property_location
  28. )"
  29. );
  30. $stmt->execute([
  31. 'name' => $name,
  32. 'contact_number' => $contactNumber,
  33. 'email' => $email,
  34. 'product_type' => $productType,
  35. 'internal_control_number' => $internalControlNumber,
  36. 'product_quantity' => $productQuantity,
  37. 'property_name' => $propertyName,
  38. 'property_location' => $propertyLocation,
  39. ]);
  40. $id = (int)$this->pdo->lastInsertId();
  41. $stmt = $this->pdo->prepare("SELECT * FROM cpr_preregistration WHERE cpr_id = :id");
  42. $stmt->execute(['id' => $id]);
  43. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  44. if (!$row) {
  45. throw new \RuntimeException('Failed to fetch inserted CPR pre-registration record');
  46. }
  47. return $row;
  48. }
  49. }