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

Commit

Permalink
Fixed a minor bug in the base id selection for the relations update
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Pedretti committed Nov 21, 2017
1 parent 03aefea commit 6d8a03b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ class Endpoint extends BaseEndpoint
*/
public function create(array $data)
{
return $this->request('post', '', ['json' => $data]);
$relations = !empty($data['relations']) ? $data['relations'] : [];

unset($data['relations']);

$result = $this->request('post', '', ['json' => $data]);

foreach ($relations as $relation) {
if (!empty($relation['id'])) {
$this->related($result->id)->create($relation['id'], $relation['type']);
}
}

return $result;
}

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

foreach ($results as $result) {
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
if (!empty($relation['id'])) {
$this->related($result->id)->create($relation['id'], $relation['type']);
if (!empty($relations[$result->custom_fields['TM2 ID']->getValue()])) {
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
if (!empty($relation['id'])) {
$this->related($result->id)->create($relation['id'], $relation['type']);
}
}
}
}
Expand Down Expand Up @@ -135,7 +149,7 @@ public function search(array $params = [], int $size = 200, int $page = null)
*/
public function edit(int $id, array $data)
{
$relations = $data['relations'];
$relations = !empty($data['relations']) ? $data['relations'] : [];

unset($data['relations']);

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

foreach ($results as $result) {
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
$this->related($result->id)->create($relation['id'], $relation['type']);
if (!empty($relations[$result->custom_fields['TM2 ID']->getValue()])) {
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
$this->related($result->id)->create($relation['id'], $relation['type']);
}
}
}

Expand Down

0 comments on commit 6d8a03b

Please sign in to comment.