public function save() { $attribute = Attribute::model()->findByPk($this->id); if (is_null($attribute)) { // is insert $attribute = new Attribute(); $attribute->attribute_group_id = $this->group; $attribute->sort_order = $this->sortOrder; $attribute->save(); $attributeDescription = new AttributeDescription(); $attributeDescription->attribute_id = $attribute->attribute_id; $attributeDescription->language_id = 1; // TODO: read locale $attributeDescription->name = $this->name; $attributeDescription->save(); } else { $attribute->attribute_group_id = $this->group; $attribute->sort_order = $this->sortOrder; $attribute->save(); $attribute->description->name = $this->name; $attribute->description->save(); } }
public function actionCreate() { $model = new Attribute(); $description = new AttributeDescription(); $this->performAjaxValidation(array($model, $description), 'attribute-form'); if (isset($_POST[$this->modelName])) { $model->setAttributes($_POST[$this->modelName]); $description->setAttributes($_POST[$this->modelName . 'Description']); $suc = Yii::t('info', 'Attribute was successfully created'); $err = Yii::t('info', 'Could not update Attribute'); $description->attribute_id = 0; $description->locale_code = Yii::app()->getLanguage(); if ($model->validate() && $description->validate()) { if ($model->save()) { $description->attribute_id = $model->id; $description->save(); Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_SUCCESS, $suc); if (Yii::app()->getRequest()->getIsAjaxRequest()) { $this->renderPartial('_view', array('model' => $model, 'description' => $description), false, true); Yii::app()->end(); } else { $this->redirect(array('view', 'id' => $model->id)); } } else { Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, $err); } } else { $description->validate(); } } if (Yii::app()->getRequest()->getIsAjaxRequest()) { $this->renderPartial('_form', array('model' => $model, 'description' => $description), false, true); Yii::app()->end(); } $this->render('create', array('model' => $model, 'description' => $description)); }
public function actionCreate() { $model = new Attribute; $lang_model = new LanguageModel; $AttrDescModel = new AttributeDescription; $ValueModel = new Value; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $allLangs = $lang_model->findAll('1', array('order' => 'id ASC')); $allLangsJS = array(); foreach ($allLangs as $language) { $allLangsJS[$language['id']] = $language['code']; } if (isset($_POST['Attribute'])) { $model->attributes = $_POST['Attribute']; if ($model->save()) { $pk = $model->getPrimaryKey(); foreach ($_POST['AttributeDescription']['attribute_name'] as $langId => $attrName) { $attr_desc_model = new AttributeDescription; $attr_desc_model->attributes = array( 'attribute_id' => $pk, 'attribute_name' => $attrName, 'language_id' => $langId, ); $attr_desc_model->save(); } if (isset($_POST['NewAttrValue'])) { foreach ($_POST['NewAttrValue'] as $spec_id => $new_val) { $ValueModel = new Value; $ValueModel->attributes = array( 'status' => 1, ); $ValueModel->save(); $v_id = $ValueModel->getPrimaryKey(); foreach ($new_val as $lang_id => $value) { if ($value != '') { $ValueDescription = new ValueDescription; $ValueDescription->attributes = array( 'value_id' => $v_id, 'language_id' => $lang_id, 'value_name' => $value, ); $ValueDescription->save(); } } $Attribute2value = new Attribute2value; $Attribute2value->attributes = array('attribute_id' => $pk, 'value_id' => $v_id); $Attribute2value->save(); } } $this->redirect(array('index')); } } $this->render('create', array( 'model' => $model, 'allLangs' => $allLangs, 'AttrDescModel' => $AttrDescModel, 'allLangsJS' => json_encode($allLangsJS), )); }