Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@

class MorphToMany extends EloquentMorphToMany
{
use InteractsWithPivotTable;
use ManagesRefresh;

/** {@inheritdoc} */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions src/Relations/Traits/InteractsWithPivotTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PDPhilip\Elasticsearch\Relations\Traits;

use BackedEnum;

trait InteractsWithPivotTable
{

/** {@inheritdoc} */
protected function formatRecordsList(array $records)
{
return collect($records)->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();
}

}