浏览代码

Refactoring repositories #1084

feature/souke
Guillaume Bourgeois 1年前
父节点
当前提交
9a51fb6eb2
共有 2 个文件被更改,包括 26 次插入10 次删除
  1. +10
    -10
      common/logic/Distribution/ProductDistribution/Repository/ProductDistributionRepository.php
  2. +16
    -0
      common/logic/Distribution/ProductDistribution/Repository/ProductDistributionRepositoryQuery.php

+ 10
- 10
common/logic/Distribution/ProductDistribution/Repository/ProductDistributionRepository.php 查看文件

@@ -28,23 +28,23 @@ class ProductDistributionRepository extends AbstractRepository

public function findOneProductDistribution(Distribution $distribution, Product $product): ?ProductDistribution
{
return ProductDistribution::searchOne([
'id_distribution' => $distribution->id,
'id_product' => $product->id
]);
return $this->createDefaultQuery()
->filterByDistribution($distribution)
->filterByProduct($product)
->findOne();
}

public function findProductDistributionsByDistribution(Distribution $distribution): array
{
return ProductDistribution::searchAll([
'id_distribution' => $distribution->id
]) ;
return $this->createDefaultQuery()
->filterByDistribution($distribution)
->find();
}

public function countProductDistributionsByDistribution(Distribution $distribution): int
{
return ProductDistribution::searchCount([
'id_distribution' => $distribution->id
]);
return $this->createDefaultQuery()
->filterByDistribution($distribution)
->count();
}
}

+ 16
- 0
common/logic/Distribution/ProductDistribution/Repository/ProductDistributionRepositoryQuery.php 查看文件

@@ -3,8 +3,10 @@
namespace common\logic\Distribution\ProductDistribution\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
use common\logic\Distribution\ProductDistribution\Service\ProductDistributionDefinition;
use common\logic\Product\Product\Model\Product;
use yii\db\ActiveQuery;

class ProductDistributionRepositoryQuery extends AbstractRepositoryQuery
@@ -15,4 +17,18 @@ class ProductDistributionRepositoryQuery extends AbstractRepositoryQuery
{
$this->loadDefinition(ProductDistributionDefinition::class);
}

public function filterByDistribution(Distribution $distribution): self
{
$this->andWhere(['id_distribution' => $distribution->id]);

return $this;
}

public function filterByProduct(Product $product): self
{
$this->andWhere(['id_product' => $product->id]);

return $this;
}
}

正在加载...
取消
保存