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.

72 lines
2.0KB

  1. <?php
  2. namespace common\logic;
  3. use yii\data\ActiveDataProvider;
  4. abstract class AbstractRepository extends AbstractService implements RepositoryInterface
  5. {
  6. const WITH = 'with';
  7. const JOIN_WITH = 'join_with';
  8. const ORDER_BY = 'orderby';
  9. const ATTRIBUTE_ID_PRODUCER = 'attribute_id_producer';
  10. public function loadQuery(string $serviceClass)
  11. {
  12. $this->query = $this->loadService($serviceClass);
  13. }
  14. public function createQuery()
  15. {
  16. $this->query->createQuery();
  17. $this->defaultWith();
  18. $this->defaultJoinWith();
  19. return $this->query;
  20. }
  21. public function createDefaultQuery(): RepositoryQueryInterface
  22. {
  23. $this->createQuery();
  24. $this->defaultWith();
  25. $this->defaultJoinWith();
  26. $this->defaultFilterProducerContext();
  27. $this->defaultOrderBy();
  28. return $this->query;
  29. }
  30. public function defaultWith(): void
  31. {
  32. $defaultOptions = $this->getDefaultOptionsSearch();
  33. if (is_array($defaultOptions['with']) && count($defaultOptions['with'])) {
  34. $this->query->with($defaultOptions['with']);
  35. }
  36. }
  37. public function defaultJoinWith(): void
  38. {
  39. $defaultOptions = $this->getDefaultOptionsSearch();
  40. if (is_array($defaultOptions['join_with']) && count($defaultOptions['join_with'])) {
  41. $this->query->joinWith($defaultOptions['join_with']);
  42. }
  43. }
  44. public function defaultFilterProducerContext(): void
  45. {
  46. $defaultOptions = $this->getDefaultOptionsSearch();
  47. if(isset($defaultOptions['attribute_id_producer']) && $defaultOptions['attribute_id_producer']) {
  48. $this->query->andWhere([$defaultOptions['attribute_id_producer'] => $this->getProducerContextId()]);
  49. }
  50. }
  51. public function defaultOrderBy(): void
  52. {
  53. $defaultOptions = $this->getDefaultOptionsSearch();
  54. if(isset($defaultOptions['orderby']) && $defaultOptions['orderby']) {
  55. $this->query->orderBy($defaultOptions['orderby']);
  56. }
  57. }
  58. }