我想提供一些数据。问题是该chunk
方法需要关闭才能完成这项工作。在这种情况下,有没有办法从foreach
循环中产生数据?
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)
}
});
}
我试过这个,但它只会返回最后的结果:
public function yieldRowData(): \Iterator
{
$results = [];
$this->transactionQuery
->getQuery()
->chunk(5, function(Collection $transactions) use (&$results) {
$results = $transactions;
});
foreach($results as $transactionEntity) {
yield $transactionEntity;
}
}