WeightReadingRepository.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. class WeightReadingRepository extends BaseRepository implements IWeightReadingRepository {
  3. private $_userSettingsRepo;
  4. public function __construct(IUserSettingsRepository $userSettingsRepo) {
  5. parent::__construct();
  6. $this->_userSettingsRepo=$userSettingsRepo;
  7. }
  8. public function GetAll($userId){
  9. $readings=array();
  10. $ids=self::$PDO->query("SELECT reading_id FROM weight_readings WHERE user_id=".(int)$userId)->fetchAll(PDO::FETCH_COLUMN);
  11. foreach ($ids as $id)
  12. $readings[]=new WeightReading($this->_userSettingsRepo ,$id);
  13. return $readings;
  14. }
  15. public function GetReadingsInDateRange($userId, DateTime $from, DateTime $to){
  16. $readings=array();
  17. $prep=self::$PDO->prepare("SELECT reading_id FROM weight_readings WHERE user_id=:user_id AND (`timestamp`>=:from AND `timestamp`<=:to)");
  18. $prep->execute(array(
  19. ":user_id"=>$userId,
  20. ":from"=>$from->format('Y-m-d 00:00:00'),
  21. ":to"=>$to->format('Y-m-d 23:59:59')
  22. ));
  23. $results=$prep->fetchAll(PDO::FETCH_COLUMN);
  24. foreach ($results as $id)
  25. $readings[]=new WeightReading($this->_userSettingsRepo ,$id);
  26. return $readings;
  27. }
  28. public function Delete($id) {
  29. $prep=self::$PDO->prepare("DELETE FROM weight_readings WHERE reading_id=?");
  30. $prep->execute([$id]);
  31. }
  32. }