Skip to content

Commit 07a653a

Browse files
authored
Merge pull request #65 from pdphilip/dev
Merging in bug fixes
2 parents e0c3665 + 1476f71 commit 07a653a

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/Query/Builder.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2107,12 +2107,16 @@ public function withSuffix(string $suffix): self
21072107

21082108
public function getLimit(): int
21092109
{
2110-
return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit();
2110+
return $this->getSetLimit();
21112111
}
21122112

2113-
public function getSetLimit(): ?int
2113+
public function getSetLimit(): int
21142114
{
2115-
return $this->options()->get('limit', $this->limit) ?? null;
2115+
// If a limit was explicitly set we use that over the defaults.
2116+
return $this->limit
2117+
?? $this->options()->get('limit')
2118+
?? $this->getDefaultLimit()
2119+
?? $this->connection->getDefaultLimit();
21162120
}
21172121

21182122
public function getDefaultLimit(): ?int

src/Relations/MorphToMany.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentMorphToMany;
1111
use Illuminate\Support\Arr;
12+
use PDPhilip\Elasticsearch\Relations\Traits\InteractsWithPivotTable;
1213

1314
use function array_diff;
1415
use function array_key_exists;
@@ -25,6 +26,7 @@
2526

2627
class MorphToMany extends EloquentMorphToMany
2728
{
29+
use InteractsWithPivotTable;
2830
use ManagesRefresh;
2931

3032
/** {@inheritdoc} */
@@ -164,9 +166,9 @@ public function sync($ids, $detaching = true)
164166

165167
$records = $this->formatRecordsList($ids);
166168

167-
$current = Arr::wrap($current);
168-
169-
$detach = array_diff($current, array_keys($records));
169+
// 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.
170+
$current = array_map(fn ($key) => (string) $key, Arr::wrap($current));
171+
$detach = array_diff($current, array_map(fn ($key) => (string) $key, array_keys($records)));
170172

171173
// We need to make sure we pass a clean array, so that it is not interpreted
172174
// as an associative array.
@@ -248,7 +250,8 @@ public function attach($id, array $attributes = [], $touch = true)
248250
$id = $this->parseIds($id);
249251
}
250252

251-
$id = (array) $id;
253+
// ID Must always be a string val
254+
$id = Arr::wrap((string) $id);
252255

253256
$query = $this->newRelatedQuery();
254257
$query->whereIn($this->relatedKey, $id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PDPhilip\Elasticsearch\Relations\Traits;
6+
7+
use BackedEnum;
8+
9+
trait InteractsWithPivotTable
10+
{
11+
12+
/** {@inheritdoc} */
13+
protected function formatRecordsList(array $records)
14+
{
15+
return collect($records)->mapWithKeys(function ($attributes, $id) {
16+
if (! is_array($attributes)) {
17+
[$id, $attributes] = [$attributes, []];
18+
}
19+
20+
if ($id instanceof BackedEnum) {
21+
$id = $id->value;
22+
}
23+
24+
// We have to convert all Key Ids to string values to keep it consistent in Elastic.
25+
return [(string) $id => $attributes];
26+
})->all();
27+
}
28+
29+
/** {@inheritdoc} */
30+
protected function parseIds($value)
31+
{
32+
// We have to convert all Key Ids to string values to keep it consistent in Elastic.
33+
return collect(parent::parseIds($value))->mapWithKeys(function ($value, $key) {
34+
return [(string) $key => $value];
35+
})->all();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)