/** * Creates a new Attributes model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return string|\yii\web\Response * @throws UserException */ public function actionCreate() { $model = new Attributes(); $attachedToId = Yii::$app->request->getQueryParam('object_type_id'); $attachInfo = $objectType = null; if ($attachedToId) { $objectType = ObjectTypes::getItem($attachedToId); $attachInfo = new AttributesAttached(); $attachInfo->object_type_id = $objectType->id; } if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($attachInfo) { $attachInfo->attr_id = $model->id; if ($attachInfo->load(Yii::$app->request->post()) && $attachInfo->save()) { return $this->redirect(['object-types/view', 'id' => $attachedToId]); } } else { throw new UserException(Yii::t('main', 'Attribute with id = {$id} not found.', ['id' => Yii::$app->request->getQueryParam('object_type_id')])); } return $this->redirect(['view', 'id' => $attachedToId]); } else { return $this->render('create', ['model' => $model, 'attrTypes' => $this->getAttributeTypes(), 'objectType' => $objectType, 'attachInfo' => $attachInfo]); } }