| @@ 37-60 (lines=24) @@ | ||
| 34 | * @throws NotFoundHttpException |
|
| 35 | * @throws \yii\base\InvalidConfigException |
|
| 36 | */ |
|
| 37 | public function run($id, $name) |
|
| 38 | { |
|
| 39 | /** @var BaseActiveRecord $model */ |
|
| 40 | $model = $this->findModel($id); |
|
| 41 | ||
| 42 | if (!$related = $model->getRelation($name, false)) { |
|
| 43 | throw new NotFoundHttpException('Relationship does not exist'); |
|
| 44 | } |
|
| 45 | ||
| 46 | if (!$related->multiple) { |
|
| 47 | throw new ForbiddenHttpException('Unsupported request to update relationship'); |
|
| 48 | } |
|
| 49 | ||
| 50 | if ($this->checkAccess) { |
|
| 51 | call_user_func($this->checkAccess, $this->id, $model, $name); |
|
| 52 | } |
|
| 53 | ||
| 54 | $this->unlinkRelationships($model, [$name => Yii::$app->getRequest()->getBodyParams()]); |
|
| 55 | ||
| 56 | ||
| 57 | return new ActiveDataProvider([ |
|
| 58 | 'query' => $related |
|
| 59 | ]); |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Removes the relationships from primary model. |
|
| @@ 28-50 (lines=23) @@ | ||
| 25 | * @throws BadRequestHttpException |
|
| 26 | * @throws NotFoundHttpException |
|
| 27 | */ |
|
| 28 | public function run($id, $name) |
|
| 29 | { |
|
| 30 | /** @var BaseActiveRecord $model */ |
|
| 31 | $model = $this->findModel($id); |
|
| 32 | ||
| 33 | if (!$related = $model->getRelation($name, false)) { |
|
| 34 | throw new NotFoundHttpException('Relationship does not exist'); |
|
| 35 | } |
|
| 36 | ||
| 37 | if ($this->checkAccess) { |
|
| 38 | call_user_func($this->checkAccess, $this->id, $model, $name); |
|
| 39 | } |
|
| 40 | ||
| 41 | $this->linkRelationships($model, [$name => Yii::$app->getRequest()->getBodyParams()]); |
|
| 42 | ||
| 43 | if ($related->multiple) { |
|
| 44 | return new ActiveDataProvider([ |
|
| 45 | 'query' => $related |
|
| 46 | ]); |
|
| 47 | } else { |
|
| 48 | return $related->one(); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | } |
|