/** * @param null $number * @return string * @throws NotFoundHttpException */ public function actionCreate($number = null) { $model = new Number(['scenario' => 'create']); $model->number = $number; if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('numberSaved', 'Номер успешно создан'); $view = 'update'; } else { $view = 'create'; } if (Yii::$app->request->isAjax) { return $this->renderAjax($view, ['model' => $model]); } else { throw new NotFoundHttpException("Запрашиваемая старница не найдена."); } }
public function actionNumbers() { if ($this->deleteReports) { $this->cleanCollection(Report::collectionName()); $this->cleanCollection(ReportItem::collectionName()); } $this->actionOperators(); $query = (new Query())->from($this->numberTableName); $this->stdout("Загружаю номера\n", Console::FG_BLUE, Console::BOLD); $this->stdout("Источник: таблица '{$this->numberTableName}' " . $query->count() . " записей.\nЦелевая коллекция: '" . Number::collectionName() . "'\n"); $this->cleanCollection(Number::collectionName()); $count = 0; foreach ($query->all() as $item) { $number = new Number(['number' => $item['number']]); $number->ownerId = (int) $item['owner_id']; $number->operatorId = $this->operatorJunction[$item['operator_id']]; $number->destination = $item['type'] == '1' ? Number::DESTINATION_PHONE : Number::DESTINATION_MODEM; $number->limit = $item['limit'] == '0' || $item['limit'] === null ? null : (int) $item['limit']; $options = []; if ($item['rent_date'] === null && $number->limit === null) { $options[] = Number::OPTION_TRIP; } if ($number->limit !== null && $item['accounting'] == '1') { $options[] = Number::OPTION_ACCOUNTING; } $number->options = $options; $history = []; if ($item['rent_date'] !== null && $item['owner_id'] !== null) { $history[] = ['rentDate' => new MongoDate((int) $item['rent_date']), 'ownerId' => (int) $item['owner_id']]; } $number->history = $history; $number->comment = $item['comment']; if ($number->save(false)) { $count++; } } $this->stdout("Успешно загружено {$count} записей\n", Console::BOLD, Console::FG_GREEN); $this->stdout("\n"); $this->actionDocuments(); return Controller::EXIT_CODE_NORMAL; }
public function testBeforeSave() { $this->specify("ownerId must be set before save number", function ($ownerFixture) { /** @var $owner Employee */ $owner = $this->employees($ownerFixture); $model = new Number(['scenario' => 'create']); $model->number = '1234567890'; $model->ownerName = $owner->fullName; $model->ownerPost = $owner->post; $model->operatorId = $this->operators('operator1')->getPrimaryKey(); $model->save(); $this->tester->seeInCollection($model::collectionName(), ['number' => '1234567890', 'ownerId' => $owner->getPrimaryKey()]); Number::deleteAll(['number' => '1234567890']); }, ['examples' => [['employee1'], ['employee4']]]); $this->specify("ownerId must be null if ownerName is empty string", function () { $model = $this->numbers('number1'); /** @var $model Number */ $model->setScenario('update'); $model->ownerName = ""; $model->save(); $this->tester->seeInCollection($model::collectionName(), ['number' => $model['number'], 'ownerId' => null]); }); }