/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $user = new User(['scenario' => 'create']); $profile = new Profile(); if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) { if ($user->validate() && $profile->validate()) { //$user->populateRelation('profile', $profile); if ($user->save(false)) { $user->link('profile', $profile); Yii::$app->session->setFlash('success', Module::t('users', 'User has been successfully created.')); return $this->redirect(['update', 'id' => $user->id]); } else { Yii::$app->session->setFlash('danger', Module::t('users', 'User has not been saved. Please try again!')); return $this->refresh(); } } } return $this->render('create', ['user' => $user, 'profile' => $profile]); }
private function createStudio($StudioForm, \app\modules\users\models\User $User) { if ($StudioForm->load(Yii::$app->request->post()) && $StudioForm->validate()) { // TODO обернуть в транзакцию $Studio = new Studio(); $Studio->type = $StudioForm->type; $Studio->name = $StudioForm->name; $User->country_id = $StudioForm->countryId; $User->city_id = $StudioForm->cityId; $Studio->currency_id = $StudioForm->currencyId; $Studio->custom_delivery = $StudioForm->custom_delivery; $Studio->custom_payment = $StudioForm->custom_payment; $Studio->slogan = $StudioForm->slogan; $Studio->description = $StudioForm->description; // Сохраним студию назначив ее пользователю $User->link('studio', $Studio); // Когда у Студии будет id (после записи), назначим связи многие ко многим // TODO попробовать сразу подать массив в link, т.к. запрос на каждую итерацию $deliveryObjects = Delivery::findAll($StudioForm->deliveryList); foreach ($deliveryObjects as $delivery) { $Studio->link('delivery', $delivery); } $paymentObjects = Payment::findAll($StudioForm->paymentList); foreach ($paymentObjects as $payment) { $Studio->link('payment', $payment); } // Назначим новую роль пользователю (atelierOwner или storeOwner) $User->assignRole($Studio->type . 'Owner'); // Рендерим страницу магазина или ателье return $this->redirect(['index']); } $StudioForm->fillCountry($User); $StudioForm->fillCity($User); return $this->render('create', ['studioForm' => $StudioForm]); }