|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
-
- namespace common\logic;
-
- use common\components\ActiveRecordCommon;
- use common\logic\Distribution\Distribution\Service\DistributionDefinition;
- use yii\db\ActiveQuery;
-
- abstract class AbstractRepositoryQuery extends AbstractService implements RepositoryQueryInterface
- {
- protected ActiveQuery $query;
-
- public function loadDefinition(string $serviceClass): void
- {
- $this->definition = $this->loadService($serviceClass);
- }
-
- public function baseQuery(): ActiveQuery
- {
- $class = $this->definition->getEntityFqcn();
- return $class::find();
- }
-
- public function createQuery(): self
- {
- $this->query = $this->baseQuery();
-
- return $this;
- }
-
- public function __call(string $name, $params): self
- {
- call_user_func_array([$this->query, $name], $params);
-
- return $this;
- }
-
- public function query()
- {
- return $this->query;
- }
-
- public function count()
- {
- $class = $this->definition->getEntityFqcn();
- $class::groupByPrimaryKey($class, $this->query);
-
- return $this->query->count();
- }
-
- public function find()
- {
- return $this->query->all();
- }
-
- public function findOne()
- {
- return $this->query->one();
- }
-
- public function filterById(int $id): self
- {
- $class = $this->definition->getEntityFqcn();
- $this->query->andWhere([$class::tableName().'.id' => $id]);
-
- return $this;
- }
-
- public function filterByCondition(string $condition = ''): self
- {
- if($condition && strlen($condition) > 0) {
- $this->andWhere($condition);
- }
-
- return $this;
- }
- }
|