/** * 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]); } }
/** * Attach attributes to object type. * @param string $id List of attributes ids like '1,2,3' * @param string $object_type_id Object type id * * @return \yii\web\Response * @throws NotFoundHttpException */ public function actionAttachAttributes($id, $object_type_id) { // TODO: Replace foreach on multiple insert query $attributeIds = explode(',', $id); foreach ($attributeIds as $id) { $model = new AttributesAttached(); $model->attr_id = $id; $model->object_type_id = $object_type_id; $model->save(); } return $this->redirect(Yii::$app->request->getReferrer()); }
public function attachAttribute($attributeId) { $model = new AttributesAttached(); $model->object_type_id = $this->id; $model->attr_id = $attributeId; return $model->save(); }