diff --git a/src/GenericModel.php b/src/GenericModel.php index 7e7ea91..30c3b11 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->shouldSaveResultsToRegistry()) { 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 $saveResultsToRegistry 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 $saveResultsToRegistry = true): QueryResult { - return static::query(new SelectQuery($where, $order, $fields, $limit, $group)); + return static::query(new SelectQuery($where, $order, $fields, $limit, $group, $saveResultsToRegistry)); } /** @@ -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..0d266f2 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 $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) + null|array $group = null, + protected bool $saveResultsToRegistry = true) { if ($where) { $this->where($where); @@ -97,4 +99,26 @@ public function getGroup(): ?array { return $this->group; } -} \ No newline at end of file + + /** + * Set whether results of this query should be saved in the model registry + * + * @param bool $saveResultsToRegistry + * @return $this + */ + public function saveResultsToRegistry(bool $saveResultsToRegistry = true): static + { + $this->saveResultsToRegistry = $saveResultsToRegistry; + return $this; + } + + /** + * Whether results of this query should be saved in the model registry + * + * @return bool + */ + public function shouldSaveResultsToRegistry(): bool + { + return $this->saveResultsToRegistry; + } +}