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 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(); + } + + }