/** * Finds the Employees model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Employees the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Employees::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Обрабатываем загруженный файл */ public function actionReadFile() { $filename = 'in/cellnumbers.txt'; //$fsz = filesize($filename); $readfile = fopen($filename, 'r'); $err = ''; //инвертируем статус. номера, не прошедшие обработку, останутся с отрицательным статусом Yii::$app->db->createCommand('UPDATE cell_numbers SET status = status*(-1)')->execute(); while ($str = fgets($readfile, 1024)) { //$items = explode(chr(9), $str); $items = explode(";", $str); if (count($items) != 3) { Yii::$app->db->createCommand('UPDATE cell_numbers SET status = status*(-1)')->execute(); //возвращаем статусы Yii::$app->session->setFlash('error', 'Файл не соответствует формату'); break; } if (stristr($str, 'Абонент') > '') { continue; } $str_cell = str_replace('-', '', $items[2]); $cell = CellNumbers::findOne(['cell_number' => $str_cell]); $emp = Employees::findOne(['snp' => $items[0]]); if (isset($cell)) { if (!isset($emp)) { continue; } if ($cell->employee_id != $emp->id) { $cell_count = CellNumbers::find()->where(['employee_id' => $emp->id])->count(); $cell->employee_id = $emp->id; $cell->status = $cell_count + 1; $cell->save(); } else { $cell->status = $cell->status * -1; if ($cell->validate()) { $cell->save(); } else { //если не прошли валидацию - собираем ошибки foreach ($cell->errors as $key => $error) { $err .= '<b>' . $key . '</b> ' . implode(' ', $error) . '<br>'; } Yii::$app->session->setFlash('error', $err); } } } else { $cell = new CellNumbers(); if ($emp) { $cell_count = CellNumbers::find()->where(['employee_id' => $emp->id])->count(); $cell->status = $cell_count + 1; $cell->employee_id = $emp->id; } $cell->cell_number = $str_cell; $cell->save(); } } //номерам, не попавшим в обработку (статус < 0), удаляем владельцев и сбрасываем статус на 1 Yii::$app->db->createCommand('UPDATE cell_numbers SET employee_id = NULL, status = 1 WHERE status < 0')->execute(); }