Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -511,16 +511,18 @@ 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>|static[]
* @noinspection PhpDocSignatureInspection
*/
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));
}

/**
Expand Down Expand Up @@ -697,4 +699,4 @@ public static function search(Search $search): SearchResult

return $result;
}
}
}
28 changes: 26 additions & 2 deletions src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -97,4 +99,26 @@ public function getGroup(): ?array
{
return $this->group;
}
}

/**
* 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;
}
}
Loading