-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from dnna/efficientorderexport
Make order export more efficient when using ORM
- Loading branch information
Showing
7 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfSylius\SyliusImportExportPlugin\Exporter\ORM\Hydrator; | ||
|
||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
|
||
interface HydratorInterface | ||
{ | ||
/** | ||
* @param int[]|string[] $idsToExport | ||
* | ||
* @return ResourceInterface[] | ||
*/ | ||
public function getHydratedResources(array $idsToExport): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfSylius\SyliusImportExportPlugin\Exporter\ORM\Hydrator; | ||
|
||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Query; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
final class OrderHydrator implements HydratorInterface | ||
{ | ||
/** | ||
* @var RepositoryInterface | ||
*/ | ||
private $repository; | ||
|
||
public function __construct( | ||
RepositoryInterface $repository | ||
) { | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHydratedResources(array $idsToExport): array | ||
{ | ||
if (!$this->repository instanceof \Doctrine\ORM\EntityRepository) { | ||
/** @var ResourceInterface[] $items */ | ||
$items = $this->repository->findBy(['id' => $idsToExport]); | ||
|
||
return $items; | ||
} | ||
|
||
$query = $this->findOrdersQb($idsToExport)->getQuery(); | ||
$items = $this->enableEagerLoading($query)->getResult(); | ||
$this->hydrateOrderItemsQb($idsToExport)->getQuery()->getResult(); // This result can be discarded | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* @param int[]|string[] $idsToExport | ||
*/ | ||
private function findOrdersQb(array $idsToExport): QueryBuilder | ||
{ | ||
/** | ||
* @var \Doctrine\ORM\EntityRepository | ||
*/ | ||
$repository = $this->repository; | ||
|
||
return $repository->createQueryBuilder('o') | ||
->andWhere('o.id IN (:exportIds)') | ||
->setParameter('exportIds', $idsToExport) | ||
; | ||
} | ||
|
||
/** | ||
* @param int[]|string[] $idsToExport | ||
*/ | ||
private function hydrateOrderItemsQb(array $idsToExport): QueryBuilder | ||
{ | ||
/** | ||
* @var \Doctrine\ORM\EntityRepository | ||
*/ | ||
$repository = $this->repository; | ||
|
||
// Partial hydration to make sure order items don't get lazy-loaded | ||
return $repository->createQueryBuilder('o') | ||
->select('PARTIAL o.{id}, items') | ||
->leftJoin('o.items', 'items') | ||
->andWhere('o.id IN (:exportIds)') | ||
->setParameter('exportIds', $idsToExport) | ||
; | ||
} | ||
|
||
private function enableEagerLoading(Query $query): Query | ||
{ | ||
return $query | ||
->setFetchMode( | ||
$this->repository->getClassName(), | ||
'customer', | ||
ClassMetadata::FETCH_EAGER | ||
) | ||
->setFetchMode( | ||
$this->repository->getClassName(), | ||
'shippingAddress', | ||
ClassMetadata::FETCH_EAGER | ||
) | ||
->setFetchMode( | ||
$this->repository->getClassName(), | ||
'billingAddress', | ||
ClassMetadata::FETCH_EAGER | ||
) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters