Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AbstractRepositoryQuery.php 2.5KB

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