Skip to content

Commit 36bad0b

Browse files
fix: relationship functions
1 parent 534c91b commit 36bad0b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/relationship.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ function manyToOne($many, $one, $property) {
77
$result = [];
88

99
foreach ($many as $manyItem):
10-
$filter = array_filter($one, fn($item) => $manyItem[$property] == $item['id']);
10+
11+
// Object Convert to Array
12+
$manyItem = (array) $manyItem;
13+
14+
$filter = array_filter($one, fn($item) => $manyItem[$property] == $item->id);
1115
$manyItem[$property] = [...$filter][0];
1216
array_push($result, $manyItem);
1317
endforeach;
@@ -21,13 +25,16 @@ function oneToMany($one, $many, $property) {
2125

2226
foreach ($one as $oneItem):
2327

28+
// Object Convert to Array
29+
$oneItem = (array) $oneItem;
30+
2431
if ($oneItem[$property] == null) $oneItem[$property] = [];
2532
else {
2633
$ids = json_decode($oneItem[$property]);
2734
$oneItem[$property] = [];
2835

2936
foreach ($ids as $id):
30-
$array = array_filter($many, fn($item) => $id == $item['id']);
37+
$array = array_filter($many, fn($item) => $id == $item->id);
3138
array_push($oneItem[$property], ...$array);
3239
endforeach;
3340
}
@@ -43,10 +50,10 @@ function oneWayFilter($current, $filter, $addProperty, $findProperty) {
4350
$result = [];
4451

4552
foreach ($current as $currentItem):
46-
$currentItem[$addProperty] = [];
53+
$currentItem->$addProperty = [];
4754

48-
$array = array_filter($filter, fn($item) => $currentItem['id'] == $item[$findProperty]);
49-
array_push($currentItem[$addProperty], ...$array);
55+
$array = array_filter($filter, fn($item) => $currentItem->id == $item->$findProperty);
56+
array_push($currentItem->$addProperty, ...$array);
5057
array_push($result, $currentItem);
5158
endforeach;
5259

0 commit comments

Comments
 (0)