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.

AbstractRepositoryQuery.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace common\logic;
  3. use common\components\ActiveRecordCommon;
  4. use common\logic\Distribution\Distribution\Service\DistributionDefinition;
  5. use yii\db\ActiveQuery;
  6. abstract class AbstractRepositoryQuery extends AbstractService implements RepositoryQueryInterface
  7. {
  8. protected ActiveQuery $query;
  9. public function loadDefinition(string $serviceClass): void
  10. {
  11. $this->definition = $this->loadService($serviceClass);
  12. }
  13. public function baseQuery(): ActiveQuery
  14. {
  15. $class = $this->definition->getEntityFqcn();
  16. return $class::find();
  17. }
  18. public function createQuery(): self
  19. {
  20. $this->query = $this->baseQuery();
  21. return $this;
  22. }
  23. public function __call(string $name, $params): self
  24. {
  25. call_user_func_array([$this->query, $name], $params);
  26. return $this;
  27. }
  28. public function query()
  29. {
  30. return $this->query;
  31. }
  32. public function count()
  33. {
  34. $class = $this->definition->getEntityFqcn();
  35. $class::groupByPrimaryKey($class, $this->query);
  36. return $this->query->count();
  37. }
  38. public function find()
  39. {
  40. return $this->query->all();
  41. }
  42. public function findOne()
  43. {
  44. return $this->query->one();
  45. }
  46. public function filterById(int $id): self
  47. {
  48. $class = $this->definition->getEntityFqcn();
  49. $this->query->andWhere([$class::tableName().'.id' => $id]);
  50. return $this;
  51. }
  52. public function filterByCondition(string $condition = ''): self
  53. {
  54. if($condition && strlen($condition) > 0) {
  55. $this->andWhere($condition);
  56. }
  57. return $this;
  58. }
  59. }