shopware-php-sdk
shopware-php-sdk copied to clipboard
Iterate to many entitys
What is the common way to iterate through many entities, for example a thousand products or more?
Now i do it in this way, but it feels not good, what is your solution?
private function getProducts(): \Generator
{
$criteria = new Criteria();
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
do {
$result = $productRepository->search($criteria, $this->getContext());
/** @var ProductEntity $product */
foreach ($result->getEntities() as $product) {
yield $product;
}
$criteria->setPage($criteria->getPage() + 1);
} while (($result->count()) === $criteria->getLimit());
}
if i remove the limit with $criteria->setLimit(null);
i got the Allowed memory size
error
Hi @oliverkuehne Thanks for the feedback, I will take a deeper look into this and give my idea soon. Any other suggestion from anyone is welcome :)
Hi @okuehne The Collection have built-in getIterator() so I guess you don't need to define your own method. As for large array, I'd suggest to either chunk it into smaller chunk of products or fetch the products with offset and limit, like 100 products per request.