You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.4KB

  1. <?php
  2. namespace domain\_;
  3. use yii\data\ActiveDataProvider;
  4. use yii\db\ActiveQuery;
  5. abstract class AbstractRepositoryQuery extends AbstractService implements RepositoryQueryInterface
  6. {
  7. protected ActiveQuery $query;
  8. public function loadDefinition(string $serviceClass): void
  9. {
  10. $this->definition = $this->loadService($serviceClass);
  11. }
  12. public function getDefinition()
  13. {
  14. return $this->definition;
  15. }
  16. public function baseQuery(): ActiveQuery
  17. {
  18. $class = $this->definition->getEntityFqcn();
  19. return $class::find();
  20. }
  21. public function createQuery(): self
  22. {
  23. $this->query = $this->baseQuery();
  24. return $this;
  25. }
  26. public function __call(string $name, $params): self
  27. {
  28. call_user_func_array([$this->query, $name], $params);
  29. return $this;
  30. }
  31. public function query()
  32. {
  33. return $this->query;
  34. }
  35. public function count()
  36. {
  37. $class = $this->definition->getEntityFqcn();
  38. $class::groupByPrimaryKey($class, $this->query);
  39. return $this->query->count();
  40. }
  41. public function find()
  42. {
  43. return $this->query->all();
  44. }
  45. public function findOne()
  46. {
  47. return $this->query->one();
  48. }
  49. public function filterById(int $id): self
  50. {
  51. $class = $this->definition->getEntityFqcn();
  52. $this->query->andWhere([$class::tableName().'.id' => $id]);
  53. return $this;
  54. }
  55. public function filterByCondition(string $condition = ''): self
  56. {
  57. if($condition && strlen($condition) > 0) {
  58. $this->andWhere($condition);
  59. }
  60. return $this;
  61. }
  62. public function filterIsStatusOnline()
  63. {
  64. $this->andWhere(['status' => StatusInterface::STATUS_ONLINE]);
  65. return $this;
  66. }
  67. public function filterIsStatusOnlineAndOffline()
  68. {
  69. $this->andWhere('status >= :status')->addParams(['status' => StatusInterface::STATUS_OFFLINE]);
  70. return $this;
  71. }
  72. public function filterIsStatusDeleted()
  73. {
  74. $this->andWhere(['status' => StatusInterface::STATUS_DELETED]);
  75. return $this;
  76. }
  77. public function getDataProvider(int $pageSize): ActiveDataProvider
  78. {
  79. return new ActiveDataProvider([
  80. 'query' => $this->query,
  81. 'sort' => false,
  82. 'pagination' => [
  83. 'pageSize' => $pageSize,
  84. ],
  85. ]);
  86. }
  87. }