123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Repository;
- use App\Model\Drug;
- class DrugRepository extends RepositoryAbstract
- {
- public function __construct()
- {
- parent::__construct();
- $this->entityName = 'drug';
- }
- /**
- * @param $id
- * @return null|Drug
- */
- public function findById($id)
- {
- $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName} WHERE Id = :id");
- $stmt->execute(['id' => $id]);
- foreach ($stmt as $row) {
- return new Drug($row['Name'], $row['GroupDrug'], $row['Id']);
- }
- return null;
- }
- /**
- * @return Drug[] array
- */
- public function findAll(): array
- {
- $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName}");
- $stmt->execute();
- $drugs = [];
- foreach ($stmt as $row) {
- $drugs[] = new Drug($row['Name'], $row['GroupDrug'], $row['Id']);
- }
- return $drugs;
- }
- /**
- * @param Drug $drug
- */
- public function save(Drug $drug)
- {
- $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (Name, GroupDrug) VALUES(:Name, :Group)");
- $stmt->execute([
- 'Name' => $drug->name,
- 'Group' => $drug->group
- ]);
- }
- /**
- * @param Drug $drug
- */
- public function update(Drug $drug)
- {
- $stmt = $this->pdo->prepare("UPDATE {$this->entityName} SET Name = :Name, GroupDrug = :Group WHERE Id = :id");
- $stmt->execute([
- 'id' => $drug->id,
- 'Name' => $drug->name,
- 'Group' => $drug->group
- ]);
- }
- /**
- * @param int $id
- */
- public function delete(int $id)
- {
- $stmt = $this->pdo->prepare("DELETE FROM {$this->entityName} WHERE Id = :id");
- $stmt->execute(['id' => $id]);
- }
- }
|