/** * actionEditComponent * @access public * @return void */ public function actionEditComponent() { Yii::log("actionEditComponent DesignController called", "trace", self::LOG_CAT); $component = new ComponentHead(); $componentDetails = new ComponentDetails(); $dataArray = []; $dataArray['formType'] = 'Edit'; $model = new DesignForm(); $attributeArray = []; if (empty($_GET['id'])) { Yii::app()->user->setFlash('error', Yii::t("translation", "Please select a component to edit")); $this->redirect(['design/listComponents']); } if (!empty(Yii::app()->session['surDesign'])) { //fetch the form data $fetchComponentData = Yii::app()->db->createCommand()->select('cd.componentDetailId, cd.componentId, cd.subFormId, cd.value, sd.inputName, sd.inputType,ch.componentName, op.optionId, op.label')->from('componentDetails cd')->join('surFormDetails sd', 'sd.subFormId = cd.subFormId')->join('componentHead ch', 'ch.componentId = cd.componentId')->leftJoin('options op', 'op.componentId = sd.subFormId')->where('cd.componentId =' . $_GET['id'])->queryAll(); //print_r($fetchComponentData); //arrange data in array $componentData = []; $returnArray = self::getElementsAndDynamicAttributes(); $returnArray['elements']['buttons'] = ['updateComponent' => ['type' => 'submit', 'label' => 'Update component']]; $elements = $returnArray['elements']; //$model = new ComponentsForm; $model->setProperties($returnArray['dynamicDataAttributes']); $model->setAttributeLabels($returnArray['dynamicLabels']); $model->setRules($returnArray['dynamicRules']); // update the model with the data values and add id foreach ($fetchComponentData as $dat) { $compKey = $dat['inputName'] . "_" . $dat['subFormId']; $componentData[$compKey] = ['value' => $dat['value'], 'id' => $dat['componentDetailId']]; $model->{$compKey} = $dat['value']; // add the component name to the array as well but just once if (empty($componentData['componentName'])) { $componentData['componentName'] = $dat['componentName']; $model->componentName = $dat['componentName']; } } //var_dump($model, $fetchComponentData); die; // generate the components form $form = new CForm($elements, $model); if (Yii::app()->getRequest()->getIsAjaxRequest()) { echo CActiveForm::validate([$model]); Yii::app()->end(); } //validate and save the component data if ($form->submitted('DesignForm') && $form->validate()) { //print_r($form->getModel()); die(); $component->setIsNewRecord(false); $component->componentName = $form->model->componentName; $component->frameworkId = Yii::app()->session['surDesign']['id']; $component->componentId = $_GET['id']; //save the componentHead values $component->save(); $form->model->unsetProperties(['componentName']); //var_dump($form->model); die; $componentId = $_GET['id']; // fetch the form data foreach ($form->model as $key => $val) { $componentDetails = new ComponentDetails(); // record found edit record if (!empty($componentData[$key])) { $componentDetails->setIsNewRecord(false); $componentDetails->componentDetailId = $componentData[$key]['id']; $componentDetails->componentId = $componentId; $componentDetails->value = $val; $componentDetails->save(false); // record not found add new record } else { $componentDetails->setIsNewRecord(true); $componentDetails->componentDetailId = null; $componentDetails->componentId = $componentId; $params = explode("_", $key); $componentDetails->subFormId = $params[1]; $componentDetails->value = $val; $componentDetails->save(); } } Yii::app()->user->setFlash('success', Yii::t("translation", "Component successfully updated")); $this->redirect(['listComponents']); } $this->render('component', ['model' => $model, 'dataArray' => $dataArray, 'form' => $form]); } }