@@ -507,15 +507,17 @@ public function getPdo(): PDOStatement
507507 *
508508 * @param bool $unbuffered 是否开启无缓冲查询(仅限mysql)
509509 *
510- * @return \Generator
510+ * @return LazyCollection
511511 */
512- public function cursor (bool $ unbuffered = false )
512+ public function cursor (bool $ unbuffered = false ): LazyCollection
513513 {
514514 $ connection = clone $ this ->connection ;
515515
516- return $ connection ->cursor ($ this , $ unbuffered );
516+ return new LazyCollection (function () use ($ connection , $ unbuffered ) {
517+ yield from $ connection ->cursor ($ this , $ unbuffered );
518+ });
517519 }
518-
520+
519521 /**
520522 * 流式处理查询结果
521523 *
@@ -527,10 +529,11 @@ public function cursor(bool $unbuffered = false)
527529 public function stream (callable $ callback , bool $ unbuffered = false ): int
528530 {
529531 $ count = 0 ;
530- foreach ($ this ->cursor ($ unbuffered ) as $ item ) {
531- $ callback ($ item );
532- $ count ++;
533- }
532+ $ this ->cursor ($ unbuffered )
533+ ->each (function ($ item ) use ($ callback , &$ count ) {
534+ $ callback ($ item );
535+ $ count ++;
536+ });
534537 return $ count ;
535538 }
536539
@@ -610,7 +613,7 @@ public function chunk(int $count, callable $callback, string | array | null $col
610613 * @param string $order 字段排序
611614 * @return LazyCollection
612615 */
613- public function lazy (int $ count = 1000 , ?string $ column = null , string $ order = 'desc ' )
616+ public function lazy (int $ count = 1000 , ?string $ column = null , string $ order = 'desc ' ): LazyCollection
614617 {
615618 if ($ count < 1 ) {
616619 throw new Exception ('The chunk size should be at least 1 ' );
0 commit comments