From 98c1cc77f4680a028fb72682dd80252a4aedcfdc Mon Sep 17 00:00:00 2001 From: David Philip Date: Thu, 17 Apr 2025 00:42:27 +0200 Subject: [PATCH 1/2] Set limit logic back + type case bug on morph --- src/Query/Builder.php | 10 +++------- src/Relations/MorphToMany.php | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 3355eb5..7b07981 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -2107,16 +2107,12 @@ public function withSuffix(string $suffix): self public function getLimit(): int { - return $this->getSetLimit(); + return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit(); } - public function getSetLimit(): int + public function getSetLimit(): ?int { - // If a limit was explicitly set we use that over the defaults. - return $this->limit - ?? $this->options()->get('limit') - ?? $this->getDefaultLimit() - ?? $this->connection->getDefaultLimit(); + return $this->options()->get('limit', $this->limit) ?? null; } public function getDefaultLimit(): ?int diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index 16c50a2..d60c29f 100644 --- a/src/Relations/MorphToMany.php +++ b/src/Relations/MorphToMany.php @@ -251,7 +251,7 @@ public function attach($id, array $attributes = [], $touch = true) } // ID Must always be a string val - $id = Arr::wrap((string) $id); + $id = Arr::wrap($id); $query = $this->newRelatedQuery(); $query->whereIn($this->relatedKey, $id); From 46ef03d41c4f8688e8cba778ea4530e447041320 Mon Sep 17 00:00:00 2001 From: David Philip Date: Thu, 17 Apr 2025 00:43:01 +0200 Subject: [PATCH 2/2] Clean up --- src/Relations/MorphToMany.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index d60c29f..ebca867 100644 --- a/src/Relations/MorphToMany.php +++ b/src/Relations/MorphToMany.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentMorphToMany; use Illuminate\Support\Arr; +use PDPhilip\Elasticsearch\Eloquent\Model as EsModel; use PDPhilip\Elasticsearch\Relations\Traits\InteractsWithPivotTable; use function array_diff; @@ -80,7 +81,7 @@ public function addEagerConstraints(array $models) protected function setWhere() { if ($this->getInverse()) { - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $ids = $this->extractIds((array) $this->parent->{$this->table}); $this->query->whereIn($this->relatedKey, $ids); @@ -89,7 +90,7 @@ protected function setWhere() ->whereIn($this->foreignPivotKey, (array) $this->parent->{$this->parentKey}); } } else { - match (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + match (EsModel::isElasticsearchModel($this->parent)) { true => $this->query->whereIn($this->relatedKey, (array) $this->parent->{$this->relatedPivotKey}), false => $this->query ->whereIn($this->getQualifiedForeignPivotKeyName(), (array) $this->parent->{$this->parentKey}), @@ -143,7 +144,7 @@ public function sync($ids, $detaching = true) // in this joining table. We'll spin through the given IDs, checking to see // if they exist in the array of current ones, and if not we will insert. if ($this->getInverse()) { - $current = match (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + $current = match (EsModel::isElasticsearchModel($this->parent)) { true => $this->parent->{$this->table} ?: [], false => $this->parent->{$this->relationName} ?: [], }; @@ -154,7 +155,7 @@ public function sync($ids, $detaching = true) $current = $this->extractIds($current); } } else { - $current = match (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + $current = match (EsModel::isElasticsearchModel($this->parent)) { true => $this->parent->{$this->relatedPivotKey} ?: [], false => $this->parent->{$this->relationName} ?: [], }; @@ -216,7 +217,7 @@ public function attach($id, array $attributes = [], $touch = true) if ($this->getInverse()) { // Attach the new ids to the parent model. - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $this->parent->push($this->table, [ [ $this->relatedPivotKey => $model->{$this->relatedKey}, @@ -239,7 +240,7 @@ public function attach($id, array $attributes = [], $touch = true) ], true); // Attach the new ids to the parent model. - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $this->parent->push($this->relatedPivotKey, (array) $id, true); } else { $this->addIdToParentRelationData($id); @@ -261,7 +262,7 @@ public function attach($id, array $attributes = [], $touch = true) $query->push($this->foreignPivotKey, $this->parent->{$this->parentKey}); // Attach the new ids to the parent model. - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { foreach ($id as $item) { $this->parent->push($this->table, [ [ @@ -285,7 +286,7 @@ public function attach($id, array $attributes = [], $touch = true) ], true); // Attach the new ids to the parent model. - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $this->parent->push($this->relatedPivotKey, $id, true); } else { foreach ($id as $item) { @@ -328,7 +329,7 @@ public function detach($ids = [], $touch = true) ]; } - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $this->parent->pull($this->table, $data); } else { $value = $this->parent->{$this->relationName} @@ -345,7 +346,7 @@ public function detach($ids = [], $touch = true) $query->pull($this->foreignPivotKey, $this->parent->{$this->parentKey}); } else { // Remove the relation from the parent. - if (\PDPhilip\Elasticsearch\Eloquent\Model::isElasticsearchModel($this->parent)) { + if (EsModel::isElasticsearchModel($this->parent)) { $this->parent->pull($this->relatedPivotKey, $ids); } else { $value = $this->parent->{$this->relationName}