From e4a517fe06d37abd74eede6216210ed9759f3d5b Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Thu, 24 Jul 2025 11:47:16 +0200 Subject: [PATCH 1/4] Add option to prevent saving results of select query in model registry --- src/GenericModel.php | 10 ++++++---- src/Query/SelectQuery.php | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/GenericModel.php b/src/GenericModel.php index 7e7ea91..0d6b95c 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -482,7 +482,7 @@ public static function query(Query $query): QueryResult } if (static::$registry) { - if ($query instanceof SelectQuery && $result->wasSuccessful() && count($result) > 0) { + if ($query instanceof SelectQuery && $result->wasSuccessful() && $query->shouldSaveResults()) { foreach ($result as $model) { if ($model->getId() === null) { continue; @@ -511,6 +511,7 @@ public static function query(Query $query): QueryResult * @param array|null $fields * @param array|int|Limit|null $limit * @param array|GroupField[]|string[]|null $group + * @param bool $saveResults Whether results of this query should be saved in the model registry. * @return QueryResult|static[] * @noinspection PhpDocSignatureInspection */ @@ -518,9 +519,10 @@ public static function select(null|WhereCondition|array|WhereGroup $where = null null|array $order = null, null|array $fields = null, null|Limit|array|int $limit = null, - null|array $group = null): QueryResult + null|array $group = null, + bool $saveResults = true): QueryResult { - return static::query(new SelectQuery($where, $order, $fields, $limit, $group)); + return static::query(new SelectQuery($where, $order, $fields, $limit, $group, $saveResults)); } /** @@ -697,4 +699,4 @@ public static function search(Search $search): SearchResult return $result; } -} \ No newline at end of file +} diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index c845c14..034fbd4 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -23,12 +23,14 @@ class SelectQuery extends Query * @param array|null $fields * @param array|int|Limit|null $limit * @param array|GroupField[]|string[]|null $group + * @param bool $saveResults Whether results of this query should be saved in the model registry. */ public function __construct(null|WhereCondition|array|WhereGroup $where = null, null|array $order = null, null|array $fields = null, null|Limit|array|int $limit = null, - null|array $group = null) + null|array $group = null, + protected bool $saveResults = true) { if ($where) { $this->where($where); @@ -97,4 +99,13 @@ public function getGroup(): ?array { return $this->group; } -} \ No newline at end of file + + /** + * Whether results of this query should be saved in the model registry + * @return bool + */ + public function shouldSaveResults(): bool + { + return $this->saveResults; + } +} From 1b981ead4887e6475e7088f6ccd6288bb0db76af Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Thu, 24 Jul 2025 11:51:21 +0200 Subject: [PATCH 2/4] Add fluent setter --- src/Query/SelectQuery.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index 034fbd4..5d3f13c 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -100,8 +100,21 @@ public function getGroup(): ?array return $this->group; } + /** + * Set whether results of this query should be saved in the model registry + * + * @param bool $saveResults + * @return $this + */ + public function saveResults(bool $saveResults = true): static + { + $this->saveResults = $saveResults; + return $this; + } + /** * Whether results of this query should be saved in the model registry + * * @return bool */ public function shouldSaveResults(): bool From c31d5c2d5b1ef90f723186d9900d8bef4f7cebcc Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Thu, 24 Jul 2025 11:58:06 +0200 Subject: [PATCH 3/4] Improve naming --- src/GenericModel.php | 6 +++--- src/Query/SelectQuery.php | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/GenericModel.php b/src/GenericModel.php index 0d6b95c..f5dc1bf 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -482,7 +482,7 @@ public static function query(Query $query): QueryResult } if (static::$registry) { - if ($query instanceof SelectQuery && $result->wasSuccessful() && $query->shouldSaveResults()) { + if ($query instanceof SelectQuery && $result->wasSuccessful() && $query->shouldSaveResultsToRegistry()) { foreach ($result as $model) { if ($model->getId() === null) { continue; @@ -520,9 +520,9 @@ public static function select(null|WhereCondition|array|WhereGroup $where = null null|array $fields = null, null|Limit|array|int $limit = null, null|array $group = null, - bool $saveResults = true): QueryResult + bool $saveResultsToRegistry = true): QueryResult { - return static::query(new SelectQuery($where, $order, $fields, $limit, $group, $saveResults)); + return static::query(new SelectQuery($where, $order, $fields, $limit, $group, $saveResultsToRegistry)); } /** diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index 5d3f13c..236c1b9 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -23,14 +23,14 @@ class SelectQuery extends Query * @param array|null $fields * @param array|int|Limit|null $limit * @param array|GroupField[]|string[]|null $group - * @param bool $saveResults Whether results of this query should be saved in the model registry. + * @param bool $saveResultsToRegistry Whether results of this query should be saved in the model registry. */ public function __construct(null|WhereCondition|array|WhereGroup $where = null, null|array $order = null, null|array $fields = null, null|Limit|array|int $limit = null, null|array $group = null, - protected bool $saveResults = true) + protected bool $saveResultsToRegistry = true) { if ($where) { $this->where($where); @@ -106,9 +106,9 @@ public function getGroup(): ?array * @param bool $saveResults * @return $this */ - public function saveResults(bool $saveResults = true): static + public function saveResultsToRegistry(bool $saveResults = true): static { - $this->saveResults = $saveResults; + $this->saveResultsToRegistry = $saveResults; return $this; } @@ -117,8 +117,8 @@ public function saveResults(bool $saveResults = true): static * * @return bool */ - public function shouldSaveResults(): bool + public function shouldSaveResultsToRegistry(): bool { - return $this->saveResults; + return $this->saveResultsToRegistry; } } From 05b66c1d0ef8ad1843dd49b9d44e27c75db40259 Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Thu, 24 Jul 2025 11:58:49 +0200 Subject: [PATCH 4/4] Fix phpdoc and parameter name --- src/GenericModel.php | 2 +- src/Query/SelectQuery.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GenericModel.php b/src/GenericModel.php index f5dc1bf..30c3b11 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -511,7 +511,7 @@ public static function query(Query $query): QueryResult * @param array|null $fields * @param array|int|Limit|null $limit * @param array|GroupField[]|string[]|null $group - * @param bool $saveResults Whether results of this query should be saved in the model registry. + * @param bool $saveResultsToRegistry Whether results of this query should be saved in the model registry. * @return QueryResult|static[] * @noinspection PhpDocSignatureInspection */ diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index 236c1b9..0d266f2 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -103,12 +103,12 @@ public function getGroup(): ?array /** * Set whether results of this query should be saved in the model registry * - * @param bool $saveResults + * @param bool $saveResultsToRegistry * @return $this */ - public function saveResultsToRegistry(bool $saveResults = true): static + public function saveResultsToRegistry(bool $saveResultsToRegistry = true): static { - $this->saveResultsToRegistry = $saveResults; + $this->saveResultsToRegistry = $saveResultsToRegistry; return $this; }