DrugRepository.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Repository;
  3. use App\Model\Drug;
  4. class DrugRepository extends RepositoryAbstract
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->entityName = 'drug';
  10. }
  11. /**
  12. * @param $id
  13. * @return null|Drug
  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 Drug($row['Name'], $row['GroupDrug'], $row['Id']);
  21. }
  22. return null;
  23. }
  24. /**
  25. * @return Drug[] array
  26. */
  27. public function findAll(): array
  28. {
  29. $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName}");
  30. $stmt->execute();
  31. $drugs = [];
  32. foreach ($stmt as $row) {
  33. $drugs[] = new Drug($row['Name'], $row['GroupDrug'], $row['Id']);
  34. }
  35. return $drugs;
  36. }
  37. /**
  38. * @param Drug $drug
  39. */
  40. public function save(Drug $drug)
  41. {
  42. $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (Name, GroupDrug) VALUES(:Name, :Group)");
  43. $stmt->execute([
  44. 'Name' => $drug->name,
  45. 'Group' => $drug->group
  46. ]);
  47. }
  48. /**
  49. * @param Drug $drug
  50. */
  51. public function update(Drug $drug)
  52. {
  53. $stmt = $this->pdo->prepare("UPDATE {$this->entityName} SET Name = :Name, GroupDrug = :Group WHERE Id = :id");
  54. $stmt->execute([
  55. 'id' => $drug->id,
  56. 'Name' => $drug->name,
  57. 'Group' => $drug->group
  58. ]);
  59. }
  60. /**
  61. * @param int $id
  62. */
  63. public function delete(int $id)
  64. {
  65. $stmt = $this->pdo->prepare("DELETE FROM {$this->entityName} WHERE Id = :id");
  66. $stmt->execute(['id' => $id]);
  67. }
  68. }