vienthuong / shopware-php-sdk

A PHP SDK for Shopware 6 Admin API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iterate to many entitys

okuehne opened this issue · 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

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.