/** * @return \yii\db\ActiveQuery */ public function getAttributesAttached() { return $this->hasOne(AttributesAttached::className(), ['attr_id' => 'id']); }
/** * Updates an existing Attributes model. * If update is successful, the browser will be redirected to the 'view' page. * * @param string $id * * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $attachInfo = $objectType = null; $attachedTo = new ArrayDataProvider(['allModels' => AttributesAttached::find()->where(['attr_id' => $model->id])->with('objectType')->all(), 'pagination' => ['pageSize' => 10]]); $attachedToId = Yii::$app->request->getQueryParam('object_type_id'); if ($attachedToId) { $objectType = ObjectTypes::getItem($attachedToId); $attachInfo = $objectType->getAttributesAttached()->where(['attr_id' => $model->id])->one(); } if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($attachInfo) { if ($attachInfo->load(Yii::$app->request->post()) && $attachInfo->save()) { return $this->redirect(Yii::$app->request->getReferrer()); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'attrTypes' => $this->getAttributeTypes(), 'attachInfo' => $attachInfo, 'objectType' => $objectType, 'attachedTo' => $attachedTo]); } }
/** * 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(); }