I would like to yield some data. Problem is that chunk
method require closure which do the job. Is there way to yield data like from foreach
loop in this situation?
public function yieldRowData(): \Iterator
{
$this->transactionQuery
->getQuery()
->chunk(5, function ($collection) { // This is executed for each 5 elements from database
foreach ($collection as $element) {
yield $element->getId(); // I want this to be yield from this method (yieldRowData)
}
});
}
I tried this, but it will return only last results:
public function yieldRowData(): \Iterator
{
$results = [];
$this->transactionQuery
->getQuery()
->chunk(5, function(Collection $transactions) use (&$results) {
$results = $transactions;
});
foreach($results as $transactionEntity) {
yield $transactionEntity;
}
}