From 85d71219c5c5e9cf27f3aa9fb783af2d56b44046 Mon Sep 17 00:00:00 2001 From: use-the-fork Date: Sun, 13 Apr 2025 11:53:19 -0400 Subject: [PATCH 1/3] bug: Keys should always cast to string to have relationships in elastic work. --- src/Relations/MorphToMany.php | 11 ++++-- .../Traits/InteractsWithPivotTable.php | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/Relations/Traits/InteractsWithPivotTable.php diff --git a/src/Relations/MorphToMany.php b/src/Relations/MorphToMany.php index 57fd09b..16c50a2 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\Relations\Traits\InteractsWithPivotTable; use function array_diff; use function array_key_exists; @@ -25,6 +26,7 @@ class MorphToMany extends EloquentMorphToMany { + use InteractsWithPivotTable; use ManagesRefresh; /** {@inheritdoc} */ @@ -164,9 +166,9 @@ public function sync($ids, $detaching = true) $records = $this->formatRecordsList($ids); - $current = Arr::wrap($current); - - $detach = array_diff($current, array_keys($records)); + // We need to make sure that all keys are processed and saved as strings since we store them as keywords a 13 != '13' this fixes that. + $current = array_map(fn ($key) => (string) $key, Arr::wrap($current)); + $detach = array_diff($current, array_map(fn ($key) => (string) $key, array_keys($records))); // We need to make sure we pass a clean array, so that it is not interpreted // as an associative array. @@ -248,7 +250,8 @@ public function attach($id, array $attributes = [], $touch = true) $id = $this->parseIds($id); } - $id = (array) $id; + // ID Must always be a string val + $id = Arr::wrap((string) $id); $query = $this->newRelatedQuery(); $query->whereIn($this->relatedKey, $id); diff --git a/src/Relations/Traits/InteractsWithPivotTable.php b/src/Relations/Traits/InteractsWithPivotTable.php new file mode 100644 index 0000000..71e5efe --- /dev/null +++ b/src/Relations/Traits/InteractsWithPivotTable.php @@ -0,0 +1,38 @@ +mapWithKeys(function ($attributes, $id) { + if (! is_array($attributes)) { + [$id, $attributes] = [$attributes, []]; + } + + if ($id instanceof BackedEnum) { + $id = $id->value; + } + + // We have to convert all Key Ids to string values to keep it consistent in Elastic. + return [(string) $id => $attributes]; + })->all(); + } + + /** {@inheritdoc} */ + protected function parseIds($value) + { + // We have to convert all Key Ids to string values to keep it consistent in Elastic. + return collect(parent::parseIds($value))->mapWithKeys(function ($value, $key) { + return [(string) $key => $value]; + })->all(); + } + + } From 4744873a9c89fa5650cd5fbb756ff2dcaf2c994c Mon Sep 17 00:00:00 2001 From: use-the-fork Date: Sun, 13 Apr 2025 11:55:05 -0400 Subject: [PATCH 2/3] bug: fix limit check. --- src/Query/Builder.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 7b07981..76add2a 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -2110,9 +2110,14 @@ public function getLimit(): int return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit(); } - public function getSetLimit(): ?int + public function getSetLimit(): int { - return $this->options()->get('limit', $this->limit) ?? null; + // If a limit was explicitly set we use that over the defaults. + if (! empty($this->limit)) { + return $this->limit; + } + + return $this->options()->get('limit', 0); } public function getDefaultLimit(): ?int From 3b6cdb8874a0498e3a86f09ce6f6ccbec2894a15 Mon Sep 17 00:00:00 2001 From: use-the-fork Date: Sun, 13 Apr 2025 11:55:05 -0400 Subject: [PATCH 3/3] bug: fix limit check. --- src/Query/Builder.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 7b07981..3355eb5 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -2107,12 +2107,16 @@ public function withSuffix(string $suffix): self public function getLimit(): int { - return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit(); + return $this->getSetLimit(); } - public function getSetLimit(): ?int + public function getSetLimit(): int { - return $this->options()->get('limit', $this->limit) ?? null; + // If a limit was explicitly set we use that over the defaults. + return $this->limit + ?? $this->options()->get('limit') + ?? $this->getDefaultLimit() + ?? $this->connection->getDefaultLimit(); } public function getDefaultLimit(): ?int