PharmacyRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Repository;
  3. use App\Model\Pharmacy;
  4. class PharmacyRepository extends RepositoryAbstract
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->entityName = 'pharmacy';
  10. }
  11. /**
  12. * @param $id
  13. * @return null|Pharmacy
  14. */
  15. public function findById($id)
  16. {
  17. $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName} WHERE Id = :id");
  18. $stmt->execute(['id' => $id]);
  19. foreach ($stmt as $row) {
  20. return new Pharmacy($row['Name'], $row['City'], $row['Street'], $row['House'], $row['Phone'], $row['Id']);
  21. }
  22. return null;
  23. }
  24. /**
  25. * @return Pharmacy[] array
  26. */
  27. public function findAll(): array
  28. {
  29. $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName}");
  30. $stmt->execute();
  31. $pharmacies = [];
  32. foreach ($stmt as $row) {
  33. $pharmacies[] = new Pharmacy($row['Name'], $row['City'], $row['Street'], $row['House'], $row['Phone'], $row['Id']);
  34. }
  35. return $pharmacies;
  36. }
  37. /**
  38. * @param Pharmacy $pharmacy
  39. */
  40. public function save(Pharmacy $pharmacy)
  41. {
  42. $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (Name, City, Street, House, Phone) VALUES(:Name, :City, :Street, :House, :Phone)");
  43. $stmt->execute([
  44. 'Name' => $pharmacy->name,
  45. 'City' => $pharmacy->city,
  46. 'Street' => $pharmacy->street,
  47. 'House' => $pharmacy->house,
  48. 'Phone' => $pharmacy->phone
  49. ]);
  50. }
  51. /**
  52. * @param Pharmacy $pharmacy
  53. */
  54. public function update(Pharmacy $pharmacy)
  55. {
  56. $stmt = $this->pdo->prepare("UPDATE {$this->entityName} SET Name = :Name, City = :City, Street = :Street, House = :House, Phone = :Phone WHERE Id = :id");
  57. $stmt->execute([
  58. 'id' => $pharmacy->id,
  59. 'Name' => $pharmacy->name,
  60. 'City' => $pharmacy->city,
  61. 'Street' => $pharmacy->street,
  62. 'House' => $pharmacy->house,
  63. 'Phone' => $pharmacy->phone
  64. ]);
  65. }
  66. /**
  67. * @param int $id
  68. */
  69. public function delete(int $id)
  70. {
  71. $stmt = $this->pdo->prepare("DELETE FROM {$this->entityName} WHERE Id = :id");
  72. $stmt->execute(['id' => $id]);
  73. }
  74. }