Skip to content

Commit e4a517f

Browse files
committed
Add option to prevent saving results of select query in model registry
1 parent 3407033 commit e4a517f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/GenericModel.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public static function query(Query $query): QueryResult
482482
}
483483

484484
if (static::$registry) {
485-
if ($query instanceof SelectQuery && $result->wasSuccessful() && count($result) > 0) {
485+
if ($query instanceof SelectQuery && $result->wasSuccessful() && $query->shouldSaveResults()) {
486486
foreach ($result as $model) {
487487
if ($model->getId() === null) {
488488
continue;
@@ -511,16 +511,18 @@ public static function query(Query $query): QueryResult
511511
* @param array|null $fields
512512
* @param array|int|Limit|null $limit
513513
* @param array|GroupField[]|string[]|null $group
514+
* @param bool $saveResults Whether results of this query should be saved in the model registry.
514515
* @return QueryResult<static>|static[]
515516
* @noinspection PhpDocSignatureInspection
516517
*/
517518
public static function select(null|WhereCondition|array|WhereGroup $where = null,
518519
null|array $order = null,
519520
null|array $fields = null,
520521
null|Limit|array|int $limit = null,
521-
null|array $group = null): QueryResult
522+
null|array $group = null,
523+
bool $saveResults = true): QueryResult
522524
{
523-
return static::query(new SelectQuery($where, $order, $fields, $limit, $group));
525+
return static::query(new SelectQuery($where, $order, $fields, $limit, $group, $saveResults));
524526
}
525527

526528
/**
@@ -697,4 +699,4 @@ public static function search(Search $search): SearchResult
697699

698700
return $result;
699701
}
700-
}
702+
}

src/Query/SelectQuery.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ class SelectQuery extends Query
2323
* @param array|null $fields
2424
* @param array|int|Limit|null $limit
2525
* @param array|GroupField[]|string[]|null $group
26+
* @param bool $saveResults Whether results of this query should be saved in the model registry.
2627
*/
2728
public function __construct(null|WhereCondition|array|WhereGroup $where = null,
2829
null|array $order = null,
2930
null|array $fields = null,
3031
null|Limit|array|int $limit = null,
31-
null|array $group = null)
32+
null|array $group = null,
33+
protected bool $saveResults = true)
3234
{
3335
if ($where) {
3436
$this->where($where);
@@ -97,4 +99,13 @@ public function getGroup(): ?array
9799
{
98100
return $this->group;
99101
}
100-
}
102+
103+
/**
104+
* Whether results of this query should be saved in the model registry
105+
* @return bool
106+
*/
107+
public function shouldSaveResults(): bool
108+
{
109+
return $this->saveResults;
110+
}
111+
}

0 commit comments

Comments
 (0)