Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate to many entitys #37

Open
okuehne opened this issue Jul 20, 2022 · 2 comments
Open

Iterate to many entitys #37

okuehne opened this issue Jul 20, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed performance

Comments

@okuehne
Copy link

okuehne commented Jul 20, 2022

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

@vienthuong vienthuong self-assigned this Jul 29, 2022
@vienthuong
Copy link
Owner

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 vienthuong added enhancement New feature or request help wanted Extra attention is needed performance labels Jul 29, 2022
@vienthuong
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed performance
Projects
None yet
Development

No branches or pull requests

2 participants