Skip to content

Commit 30f9f94

Browse files
authored
Merge pull request #64 from tuyakhov/allow_deleting_resources
Allow deleting underlying resources
2 parents adb20d7 + 3784dcd commit 30f9f94

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/ResourceTrait.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
trait ResourceTrait
1414
{
15+
/**
16+
* @var bool a flag that enables/disables deleting of the model that contains the foreign key when setting relationships
17+
* By default the model's foreign key will be set `null` and saved.
18+
*/
19+
public $allowDeletingResources = false;
20+
1521
/**
1622
* @return string
1723
*/
@@ -87,7 +93,7 @@ public function setResourceRelationship($name, $relationship)
8793
if (!is_array($relationship)) {
8894
$relationship = [$relationship];
8995
}
90-
$this->unlinkAll($name);
96+
$this->unlinkAll($name, $this->allowDeletingResources);
9197
foreach ($relationship as $key => $value) {
9298
if ($value instanceof ActiveRecordInterface) {
9399
$this->link($name, $value);

src/actions/Action.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace tuyakhov\jsonapi\actions;
77

88
use tuyakhov\jsonapi\ResourceInterface;
9+
use tuyakhov\jsonapi\ResourceTrait;
910
use yii\db\ActiveRecordInterface;
1011
use yii\db\BaseActiveRecord;
1112
use yii\helpers\ArrayHelper;
@@ -23,6 +24,11 @@ class Action extends \yii\rest\Action
2324
*/
2425
public $allowFullReplacement = true;
2526

27+
/**
28+
* @var bool Weather allow to delete the underlying resource if a relationship is deleted (as a garbage collection measure)
29+
*/
30+
public $enableResourceDeleting = false;
31+
2632
/**
2733
* Links the relationships with primary model.
2834
* @param $model ActiveRecordInterface
@@ -59,6 +65,12 @@ protected function linkRelationships($model, array $data = [])
5965
continue;
6066
}
6167
$records = $relatedClass::find()->andWhere(['in', $relatedClass::primaryKey(), $ids])->all();
68+
69+
/** @see ResourceTrait::$allowDeletingResources */
70+
if (property_exists($model, 'allowDeletingResources')) {
71+
$model->allowDeletingResources = $this->enableResourceDeleting;
72+
}
73+
6274
$model->setResourceRelationship($name, $records);
6375
}
6476
}

src/actions/DeleteRelationshipAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function unlinkRelationships($model, array $data = [])
9090

9191
$records = $relatedClass::find()->andWhere(['in', $relatedClass::primaryKey(), $ids])->all();
9292
foreach ($records as $record) {
93-
$model->unlink($name, $record);
93+
$model->unlink($name, $record, $this->enableResourceDeleting);
9494
}
9595
}
9696
}

tests/data/ResourceModel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getPrimaryKey($asArray = false)
8888
return $asArray ? [$this->getId()] : $this->getId();
8989
}
9090

91-
public function unlinkAll($name)
91+
public function unlinkAll($name, $delete = false)
9292
{
9393
$this->$name = null;
9494
}

0 commit comments

Comments
 (0)