Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

91 lignes
2.6KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use Core\Repository;
  5. class TerritoryRepository extends Repository
  6. {
  7. protected string $table = 'territories';
  8. protected string $primaryKey = 'id';
  9. public function countAll(string $search = ''): int
  10. {
  11. if ($search !== '') {
  12. $row = $this->database->first(
  13. 'SELECT COUNT(*) AS n FROM territories WHERE name LIKE :s OR description LIKE :s',
  14. ['s' => '%' . $search . '%']
  15. );
  16. } else {
  17. $row = $this->database->first('SELECT COUNT(*) AS n FROM territories');
  18. }
  19. return (int) ($row['n'] ?? 0);
  20. }
  21. /** @return list<array<string,mixed>> */
  22. public function findPaged(int $page, int $perPage, string $search = ''): array
  23. {
  24. $offset = ($page - 1) * $perPage;
  25. if ($search !== '') {
  26. return $this->database->query(
  27. 'SELECT * FROM territories WHERE name LIKE :s OR description LIKE :s
  28. ORDER BY name ASC LIMIT :limit OFFSET :offset',
  29. ['s' => '%' . $search . '%', 'limit' => $perPage, 'offset' => $offset]
  30. );
  31. }
  32. return $this->database->query(
  33. 'SELECT * FROM territories ORDER BY name ASC LIMIT :limit OFFSET :offset',
  34. ['limit' => $perPage, 'offset' => $offset]
  35. );
  36. }
  37. /** @return list<array<string,mixed>> */
  38. public function allOrdered(): array
  39. {
  40. return $this->database->query('SELECT * FROM territories ORDER BY name ASC');
  41. }
  42. public function householdCount(int|string $territoryId): int
  43. {
  44. $row = $this->database->first(
  45. 'SELECT COUNT(*) AS n FROM households WHERE territory_id = :id',
  46. ['id' => $territoryId]
  47. );
  48. return (int) ($row['n'] ?? 0);
  49. }
  50. /** @return list<array<string,mixed>> */
  51. public function householdCountsKeyed(): array
  52. {
  53. $rows = $this->database->query(
  54. 'SELECT territory_id, COUNT(*) AS n FROM households GROUP BY territory_id'
  55. );
  56. $counts = [];
  57. foreach ($rows as $row) {
  58. $counts[(int) $row['territory_id']] = (int) $row['n'];
  59. }
  60. return $counts;
  61. }
  62. /** @return list<string> */
  63. public function distinctStreets(int|string $territoryId): array
  64. {
  65. $rows = $this->database->query(
  66. 'SELECT DISTINCT street_name FROM households
  67. WHERE territory_id = :id AND street_name IS NOT NULL AND street_name != \'\'
  68. ORDER BY street_name ASC',
  69. ['id' => $territoryId]
  70. );
  71. return array_column($rows, 'street_name');
  72. }
  73. }

Powered by TurnKey Linux.