Skip to content

Commit 8ad2ae0

Browse files
authored
Merge pull request #19 from JulianVennen/select-query-should-save
Add option to prevent saving results of select query in model registry
2 parents 3407033 + 05b66c1 commit 8ad2ae0

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-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->shouldSaveResultsToRegistry()) {
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 $saveResultsToRegistry 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 $saveResultsToRegistry = 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, $saveResultsToRegistry));
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: 26 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 $saveResultsToRegistry 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 $saveResultsToRegistry = true)
3234
{
3335
if ($where) {
3436
$this->where($where);
@@ -97,4 +99,26 @@ public function getGroup(): ?array
9799
{
98100
return $this->group;
99101
}
100-
}
102+
103+
/**
104+
* Set whether results of this query should be saved in the model registry
105+
*
106+
* @param bool $saveResultsToRegistry
107+
* @return $this
108+
*/
109+
public function saveResultsToRegistry(bool $saveResultsToRegistry = true): static
110+
{
111+
$this->saveResultsToRegistry = $saveResultsToRegistry;
112+
return $this;
113+
}
114+
115+
/**
116+
* Whether results of this query should be saved in the model registry
117+
*
118+
* @return bool
119+
*/
120+
public function shouldSaveResultsToRegistry(): bool
121+
{
122+
return $this->saveResultsToRegistry;
123+
}
124+
}

0 commit comments

Comments
 (0)