shopware-php-sdk icon indicating copy to clipboard operation
shopware-php-sdk copied to clipboard

Iterate to many entitys

Open okuehne opened this issue 2 years ago • 2 comments

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

okuehne avatar Jul 20 '22 17:07 okuehne

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 :)

vienthuong avatar Jul 29 '22 14:07 vienthuong

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.

vienthuong avatar Aug 17 '22 04:08 vienthuong