-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from kbond/as-dto
fix(ORM): normalize with modifier when iterating
- Loading branch information
Showing
8 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
use Zenstruck\Collection\IterableCollection; | ||
use Zenstruck\Collection\LazyCollection; | ||
|
||
use function Zenstruck\collect; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
|
@@ -254,7 +256,13 @@ public function getIterator(): \Traversable | |
} | ||
|
||
try { | ||
yield from $this->query()->toIterable(hydrationMode: $this->hydrationMode ?? Query::HYDRATE_OBJECT); | ||
$iterator = $this->query()->toIterable(hydrationMode: $this->hydrationMode ?? Query::HYDRATE_OBJECT); | ||
|
||
if ($this->resultModifier) { | ||
$iterator = collect(fn() => yield from $iterator)->map($this->resultModifier); | ||
} | ||
|
||
yield from $iterator; | ||
} catch (QueryException $e) { | ||
if ($e->getMessage() === QueryException::iterateWithMixedResultNotAllowed()->getMessage()) { | ||
throw new \LogicException(\sprintf('Results contain aggregate fields, call %s::withAggregates().', self::class), 0, $e); | ||
|
@@ -320,7 +328,7 @@ private function paginator(?Query $query = null): Paginator | |
{ | ||
$paginator = new Paginator($query ?? $this->query(), $this->fetchJoins); | ||
|
||
if ($this->hydrationMode) { | ||
if ($this->resultModifier || $this->hydrationMode) { | ||
$paginator->setUseOutputWalkers(false); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/collection package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Collection\Tests\Doctrine\Fixture; | ||
|
||
final class EntityDto | ||
{ | ||
public function __construct(public string $value) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/collection package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Collection\Tests\Doctrine\ORM\Result; | ||
|
||
use Zenstruck\Collection\Doctrine\ORM\EntityResult; | ||
use Zenstruck\Collection\Tests\Doctrine\Fixture\Entity; | ||
use Zenstruck\Collection\Tests\Doctrine\Fixture\EntityDto; | ||
use Zenstruck\Collection\Tests\Doctrine\ORM\EntityResultTest; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class EntityDtoCallbackTest extends EntityResultTest | ||
{ | ||
protected function expectedValueAt(int $position) | ||
{ | ||
return new EntityDto((string) $position); | ||
} | ||
|
||
protected function createWithItems(int $count): EntityResult | ||
{ | ||
$this->persistEntities($count); | ||
|
||
return (new EntityResult($this->em->createQueryBuilder()->select('e.id')->from(Entity::class, 'e'))) | ||
->as(fn(array $v) => new EntityDto((string) $v['id'])) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters