From eb38acec58fbfc6e3cc7b7b59b11ffadafcca8ae Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Sat, 13 Jul 2024 14:52:58 +0700 Subject: [PATCH] feat(orm): `getQueryBuilder()` now clones the query builder, add `withQueryBuilder()` (#124) --- CHANGELOG.md | 5 +++++ packages/collections-orm/src/QueryPageable.php | 10 +++++++++- packages/collections-orm/src/QueryRecollection.php | 10 +++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed60e6..d507a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.9.2 + +* fix(orm): `getQueryBuilder()` now clones the query builder +* feat(orm): add `withQueryBuilder()` + ## 0.9.1 * fix: static analysis problem with assignment of empty `ArrayCollection` to diff --git a/packages/collections-orm/src/QueryPageable.php b/packages/collections-orm/src/QueryPageable.php index 6bafa99..876d306 100644 --- a/packages/collections-orm/src/QueryPageable.php +++ b/packages/collections-orm/src/QueryPageable.php @@ -76,7 +76,7 @@ public function withItemsPerPage(int $itemsPerPage): static final public function getQueryBuilder(): QueryBuilder { - return $this->queryBuilder; + return clone $this->queryBuilder; } /** @@ -91,6 +91,14 @@ final public function updateQueryBuilder(\Closure $function): static return $instance; } + final protected function withQueryBuilder(QueryBuilder $queryBuilder): static + { + $instance = clone $this; + $instance->queryBuilder = $queryBuilder; + + return $instance; + } + /** * @return self */ diff --git a/packages/collections-orm/src/QueryRecollection.php b/packages/collections-orm/src/QueryRecollection.php index b783ff6..0bcf6b1 100644 --- a/packages/collections-orm/src/QueryRecollection.php +++ b/packages/collections-orm/src/QueryRecollection.php @@ -109,7 +109,7 @@ public function withItemsPerPage(int $itemsPerPage): static final public function getQueryBuilder(): QueryBuilder { - return $this->queryBuilder; + return clone $this->queryBuilder; } /** @@ -124,6 +124,14 @@ final public function updateQueryBuilder(\Closure $function): static return $instance; } + final protected function withQueryBuilder(QueryBuilder $queryBuilder): static + { + $instance = clone $this; + $instance->queryBuilder = $queryBuilder; + + return $instance; + } + /** * @return self */