Skip to content
This repository was archived by the owner on Jun 1, 2021. It is now read-only.

Commit 6d8a03b

Browse files
author
Leonardo Pedretti
committed
Fixed a minor bug in the base id selection for the relations update
1 parent 03aefea commit 6d8a03b

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/Endpoints/Endpoint.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@ class Endpoint extends BaseEndpoint
1515
*/
1616
public function create(array $data)
1717
{
18-
return $this->request('post', '', ['json' => $data]);
18+
$relations = !empty($data['relations']) ? $data['relations'] : [];
19+
20+
unset($data['relations']);
21+
22+
$result = $this->request('post', '', ['json' => $data]);
23+
24+
foreach ($relations as $relation) {
25+
if (!empty($relation['id'])) {
26+
$this->related($result->id)->create($relation['id'], $relation['type']);
27+
}
28+
}
29+
30+
return $result;
1931
}
2032

2133
/** @noinspection PhpInconsistentReturnPointsInspection
@@ -60,9 +72,11 @@ public function createMany($entries)
6072
$results = $this->request('post', $this->entriesJsonifier($entries));
6173

6274
foreach ($results as $result) {
63-
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
64-
if (!empty($relation['id'])) {
65-
$this->related($result->id)->create($relation['id'], $relation['type']);
75+
if (!empty($relations[$result->custom_fields['TM2 ID']->getValue()])) {
76+
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
77+
if (!empty($relation['id'])) {
78+
$this->related($result->id)->create($relation['id'], $relation['type']);
79+
}
6680
}
6781
}
6882
}
@@ -135,7 +149,7 @@ public function search(array $params = [], int $size = 200, int $page = null)
135149
*/
136150
public function edit(int $id, array $data)
137151
{
138-
$relations = $data['relations'];
152+
$relations = !empty($data['relations']) ? $data['relations'] : [];
139153

140154
unset($data['relations']);
141155

@@ -177,8 +191,10 @@ public function editMany($entries)
177191
$results = $this->request('put', $this->entriesJsonifier($entries));
178192

179193
foreach ($results as $result) {
180-
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
181-
$this->related($result->id)->create($relation['id'], $relation['type']);
194+
if (!empty($relations[$result->custom_fields['TM2 ID']->getValue()])) {
195+
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
196+
$this->related($result->id)->create($relation['id'], $relation['type']);
197+
}
182198
}
183199
}
184200

0 commit comments

Comments
 (0)