public function delete() { //$request_param = CRequest::param(); //$this->_param = $request_param; //$this->initParam(); # admin 用户不能删除 if ($id = CRequest::param('id')) { $model = AdminUserForm::findOne(['id' => $id]); if ($model->id) { # 不允许删除admin if ($model->username == 'admin') { echo json_encode(["statusCode" => "300", "message" => 'You can not delete Admin User!']); exit; } $innerTransaction = Yii::$app->db->beginTransaction(); try { $model->delete(); AdminUserRole::deleteAll(['user_id' => $model->id]); $innerTransaction->commit(); } catch (Exception $e) { $innerTransaction->rollBack(); echo json_encode(["statusCode" => "300", "message" => 'Delete Fail !']); exit; } echo json_encode(["statusCode" => "200", "message" => 'Delete Success!']); exit; } else { echo json_encode(["statusCode" => "300", "message" => "id => {$id} , is not exist"]); exit; } } else { if ($ids = CRequest::param('ids')) { $id_arr = explode(",", $ids); # 不允许删除admin $adminUser = AdminUserForm::findOne(['username' => 'admin']); $adminUserId = $adminUser->id; if (in_array($adminUserId, $id_arr)) { echo json_encode(["statusCode" => "300", "message" => 'You can not delete Admin User!']); exit; } $innerTransaction = Yii::$app->db->beginTransaction(); try { AdminUserForm::deleteAll(['in', 'id', $id_arr]); AdminUserRole::deleteAll(['in', 'user_id', $id_arr]); $innerTransaction->commit(); } catch (Exception $e) { $innerTransaction->rollBack(); echo json_encode(["statusCode" => "300", "message" => 'Delete All Fail !']); exit; } echo json_encode(["statusCode" => "200", "message" => "{$ids} Delete Success!"]); exit; } } echo json_encode(["statusCode" => "300", "message" => "id or ids Param is not Exist!"]); exit; }
public function delete() { //$request_param = CRequest::param(); //$this->_param = $request_param; //$this->initParam(); if ($id = CRequest::param('id')) { $model = AdminUserForm::findOne(['id' => $id]); if ($model->id) { $model->delete(); echo json_encode(["statusCode" => "200", "message" => 'Delete Success!']); exit; } else { echo json_encode(["statusCode" => "300", "message" => "id => {$id} , is not exist"]); exit; } } else { if ($ids = CRequest::param('ids')) { $id_arr = explode(",", $ids); AdminUserForm::deleteAll(['in', 'id', $id_arr]); echo json_encode(["statusCode" => "200", "message" => "{$ids} Delete Success!"]); exit; } } echo json_encode(["statusCode" => "300", "message" => "id or ids Param is not Exist!"]); exit; }
public function delete() { //$request_param = CRequest::param(); //$this->_param = $request_param; //$this->initParam(); # admin 用户不能删除 if ($id = CRequest::param('id')) { $model = AdminUserForm::findOne(['id' => $id]); if ($model->id) { # 不允许删除admin if ($model->username == 'admin') { echo json_encode(["statusCode" => "300", "message" => 'You can not delete Admin User!']); exit; } $model->delete(); echo json_encode(["statusCode" => "200", "message" => 'Delete Success!']); exit; } else { echo json_encode(["statusCode" => "300", "message" => "id => {$id} , is not exist"]); exit; } } else { if ($ids = CRequest::param('ids')) { $id_arr = explode(",", $ids); # 不允许删除admin $adminUser = AdminUserForm::findOne(['username' => 'admin']); $adminUserId = $adminUser->id; if (in_array($adminUserId, $id_arr)) { echo json_encode(["statusCode" => "300", "message" => 'You can not delete Admin User!']); exit; } AdminUserForm::deleteAll(['in', 'id', $id_arr]); echo json_encode(["statusCode" => "200", "message" => "{$ids} Delete Success!"]); exit; } } echo json_encode(["statusCode" => "300", "message" => "id or ids Param is not Exist!"]); exit; }