Skip to content

Commit

Permalink
CSV出力時にメモリを使い切ってしまう問題を修正 EC-CUBE#4815
Browse files Browse the repository at this point in the history
  • Loading branch information
mako committed May 4, 2021
1 parent f7cc4a2 commit 97654ca
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/Eccube/Service/CsvExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
use Eccube\Repository\OrderRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\ShippingRepository;
use Eccube\Util\EntityUtil;
use Eccube\Util\FormUtil;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -110,15 +110,22 @@ class CsvExportService
*/
protected $formFactory;

/** @var PaginatorInterface */
protected $paginator;

/**
* CsvExportService constructor.
*
* @param EntityManagerInterface $entityManager
* @param CsvRepository $csvRepository
* @param CsvTypeRepository $csvTypeRepository
* @param OrderRepository $orderRepository
* @param ShippingRepository $shippingRepository
* @param CustomerRepository $customerRepository
* @param ProductRepository $productRepository
* @param EccubeConfig $eccubeConfig
* @param FormFactoryInterface $formFactory
* @param PaginatorInterface $paginator
*/
public function __construct(
EntityManagerInterface $entityManager,
Expand All @@ -129,7 +136,8 @@ public function __construct(
CustomerRepository $customerRepository,
ProductRepository $productRepository,
EccubeConfig $eccubeConfig,
FormFactoryInterface $formFactory
FormFactoryInterface $formFactory,
PaginatorInterface $paginator
) {
$this->entityManager = $entityManager;
$this->csvRepository = $csvRepository;
Expand All @@ -140,6 +148,7 @@ public function __construct(
$this->eccubeConfig = $eccubeConfig;
$this->productRepository = $productRepository;
$this->formFactory = $formFactory;
$this->paginator = $paginator;
}

/**
Expand Down Expand Up @@ -279,12 +288,20 @@ public function exportData(\Closure $closure)

$this->fopen();

$query = $this->qb->getQuery();
foreach ($query->getResult() as $iterableResult) {
$closure($iterableResult, $this);
$this->entityManager->detach($iterableResult);
$query->free();
flush();
$page = 1;
$limit = 100;
while ($results = $this->paginator->paginate($this->qb, $page, $limit)) {
if (!$results->valid()) {
break;
}

foreach ($results as $result) {
$closure($result, $this);
flush();
}

$this->entityManager->clear();
$page++;
}

$this->fclose();
Expand Down Expand Up @@ -317,16 +334,12 @@ public function getData(Csv $Csv, $entity)

// one to one の場合は, dtb_csv.reference_field_name, 合致する結果を取得する.
if ($data instanceof \Eccube\Entity\AbstractEntity) {
if (EntityUtil::isNotEmpty($data)) {
return $data->offsetGet($Csv->getReferenceFieldName());
}
return $data->offsetGet($Csv->getReferenceFieldName());
} elseif ($data instanceof \Doctrine\Common\Collections\Collection) {
// one to manyの場合は, カンマ区切りに変換する.
$array = [];
foreach ($data as $elem) {
if (EntityUtil::isNotEmpty($elem)) {
$array[] = $elem->offsetGet($Csv->getReferenceFieldName());
}
$array[] = $elem->offsetGet($Csv->getReferenceFieldName());
}

return implode($this->eccubeConfig['eccube_csv_export_multidata_separator'], $array);
Expand All @@ -337,8 +350,6 @@ public function getData(Csv $Csv, $entity)
// スカラ値の場合はそのまま.
return $data;
}

return null;
}

/**
Expand Down

0 comments on commit 97654ca

Please sign in to comment.