Skip to content

Commit e595628

Browse files
authored
Merge pull request #43 from tuyakhov/clear_relationships
Allow to clear relationships
2 parents 396f3f1 + d88790e commit e595628

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/JsonApiParser.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ class JsonApiParser extends JsonParser
3434
public function parse($rawBody, $contentType)
3535
{
3636
$array = parent::parse($rawBody, $contentType);
37-
$data = ArrayHelper::getValue($array, 'data', []);
38-
if (empty($data)) {
37+
if (!ArrayHelper::keyExists('data', $array)) {
3938
if ($this->throwException) {
4039
throw new BadRequestHttpException('The request MUST include a single resource object as primary data.');
4140
}
4241
return [];
4342
}
43+
$data = ArrayHelper::getValue($array, 'data', []);
44+
if (empty($data)) {
45+
return [];
46+
}
4447
if (ArrayHelper::isAssociative($data)) {
4548
$result = $this->parseResource($data);
4649

@@ -109,12 +112,14 @@ protected function parseRelationships(array $relObjects = [])
109112
{
110113
$relationships = [];
111114
foreach ($relObjects as $name => $relationship) {
112-
if (!$relData = ArrayHelper::getValue($relationship, 'data')) {
115+
if (!ArrayHelper::keyExists('data', $relationship)) {
113116
continue;
114117
}
118+
$relData = ArrayHelper::getValue($relationship, 'data', []);
115119
if (!ArrayHelper::isIndexed($relData)) {
116120
$relData = [$relData];
117121
}
122+
$relationships[$name] = [];
118123
foreach ($relData as $identifier) {
119124
if (isset($identifier['type']) && isset($identifier['id'])) {
120125
$formName = $this->typeToFormName($identifier['type']);

src/ResourceTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function setResourceRelationship($name, $relationship)
8787
if (!is_array($relationship)) {
8888
$relationship = [$relationship];
8989
}
90+
$this->unlinkAll($name);
9091
foreach ($relationship as $key => $value) {
9192
if ($value instanceof ActiveRecordInterface) {
9293
$this->link($name, $value);

src/actions/Action.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,10 @@ protected function linkRelationships($model, array $data = [])
5555
$ids[] = $relObject['id'];
5656
}
5757

58-
if (!$records = $relatedClass::find()->andWhere(['in', $relatedClass::primaryKey(), $ids])->all()) {
59-
continue;
60-
}
61-
6258
if ($related->multiple && !$this->allowFullReplacement) {
6359
continue;
6460
}
65-
$model->unlinkAll($name);
61+
$records = $relatedClass::find()->andWhere(['in', $relatedClass::primaryKey(), $ids])->all();
6662
$model->setResourceRelationship($name, $records);
6763
}
6864
}

tests/data/ResourceModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function getPrimaryKey($asArray = false)
8181
return $asArray ? [$this->getId()] : $this->getId();
8282
}
8383

84-
public function unlinkAll()
84+
public function unlinkAll($name)
8585
{
86-
return;
86+
$this->$name = null;
8787
}
8888

8989
public function save()

0 commit comments

Comments
 (0)