razonyang /
yii2-app-template
| 1 | <?php |
||
| 2 | namespace App\Http\Rest; |
||
| 3 | |||
| 4 | use App\Model\SoftDeleteInterface; |
||
| 5 | use Yii; |
||
| 6 | use yii\db\ActiveRecord; |
||
| 7 | use yii\rest\DeleteAction as Action; |
||
| 8 | |||
| 9 | class DeleteAction extends Action |
||
| 10 | { |
||
| 11 | use SafeActionTrait { |
||
| 12 | run as safeRun; |
||
| 13 | } |
||
| 14 | |||
| 15 | public function run($id) |
||
| 16 | { |
||
| 17 | return $this->safeRun($id, [$this, 'delete']); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function delete($id) |
||
| 21 | { |
||
| 22 | $model = $this->findModel($id); |
||
| 23 | |||
| 24 | if ($this->checkAccess) { |
||
| 25 | call_user_func($this->checkAccess, $this->id, $model); |
||
| 26 | } |
||
| 27 | |||
| 28 | if ($this->deleteModel($model) === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 29 | throw new ServerErrorHttpException('Failed to delete the object for unknown reason.'); |
||
|
0 ignored issues
–
show
The type
App\Http\Rest\ServerErrorHttpException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 30 | } |
||
| 31 | |||
| 32 | Yii::$app->getResponse()->setStatusCode(204); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param ActiveRecord $model |
||
| 37 | * @return int|false |
||
| 38 | */ |
||
| 39 | private function deleteModel($model) |
||
| 40 | { |
||
| 41 | if ($model instanceof SoftDeleteInterface) { |
||
| 42 | return $model->softDelete(); |
||
| 43 | } |
||
| 44 | |||
| 45 | return $model->delete(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |