public function actionDelete($id) { if (Yii::$app->request->isAjax) { if ($id == 1) { throw new HttpException(500, 'Администратора удалить нельзя'); } else { $transaction = Yii::$app->db->beginTransaction(); try { Profile::deleteAll(['profile_id' => $id]); Employee::deleteAll(['id_person' => $id]); $auth = Yii::$app->authManager; $auth->revokeAll($id); echo $this->findModel($id)->delete(); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); throw new Exception($e->getMessage()); } } } }
public function actionRemoveinactiveemployee() { $au = \app\models\Fregat\Employee::find()->select(['employee_id', 'id_person'])->groupBy(['id_person'])->all(); $del = 0; $nodel = 0; $count = count((array) $au); foreach ($au as $ar) { $inactivePerson = \app\models\Fregat\Employee::find()->andWhere(['id_person' => $ar->id_person, 'employee_dateinactive' => NULL])->count(); if (empty($inactivePerson)) { $transaction = Yii::$app->db->beginTransaction(); try { $au2 = \app\models\Config\Authuser::findOne($ar->id_person)->auth_user_fullname; \app\models\Fregat\Employee::deleteAll(['id_person' => $ar->id_person]); \app\models\Config\Authuser::deleteAll(['auth_user_id' => $ar->id_person]); Profile::deleteAll(['profile_id' => $ar->id_person]); $transaction->commit(); $del++; echo 'Deleted "' . $au2 . '"<br>'; } catch (\yii\db\IntegrityException $e) { $nodel++; echo 'Can\'t delete "' . $au2 . '"<br>'; $transaction->rollBack(); } } } echo 'Removed ' . $del . ' from ' . $count . '. Errors = ' . $nodel; }