/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CharacteristicValueModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['idProduct' => $this->idProduct, 'idCharacteristic' => $this->idCharacteristic]); $query->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
/** * Finds the CharacteristicValueModel model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $idProduct * @param integer $idCharacteristic * @return CharacteristicValueModel the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($idProduct, $idCharacteristic) { if (($model = CharacteristicValueModel::findOne(['idProduct' => $idProduct, 'idCharacteristic' => $idCharacteristic])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getCharacteristicValues() { return $this->hasMany(CharacteristicValueModel::className(), ['idCharacteristic' => 'id']); }
/** * Updates an existing ProductModel model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if (isset($_FILES['ProductModel']) && $_FILES['ProductModel']['name']['photo'] != "") { if (!in_array($_FILES['ProductModel']['type']['photo'], $this->image_array)) { $model->addError('photo', 'Avaliable file types: jpg, gif, png.'); } else { if ($model->photo != "") { unlink(Yii::getAlias('@app') . Yii::getAlias('@web') . '/' . $model->photo); } $rnd = rand(0, 9999); $uploadedFile = UploadedFile::getInstance($model, 'photo'); $fileName = 'files/' . $rnd . '_' . $uploadedFile->name; $model->photo = $fileName; $uploadedFile->saveAs($fileName); } } if (isset($_POST['CharacteristicValueModel'])) { $characteristics_values = $_POST['CharacteristicValueModel']; $success_values = true; foreach ($characteristics_values as $characteristics_value) { $temp_value = CharacteristicValueModel::find()->where(['idProduct' => $characteristics_value['idProduct'], 'idCharacteristic' => $characteristics_value['idCharacteristic']])->one(); if ($temp_value == null) { $temp_value = new CharacteristicValueModel(); $temp_value->idProduct = $characteristics_value['idProduct']; $temp_value->idCharacteristic = $characteristics_value['idCharacteristic']; } $temp_value->value = $characteristics_value['value']; if (!$temp_value->save()) { print_r($temp_value->getErrors()); $success_values = false; } } } if ($model->load(Yii::$app->request->post()) && $model->save() && $success_values) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model]); } }
<?php if (isset($model['idCategory']) && isset($model['id'])) { if (isset($model->idCategory)) { $characteristics = CharacteristicModel::find()->where(['idCategory' => $model['idCategory']])->all(); $category = CategoryModel::find()->where(['id' => $model['idCategory']])->one(); while ($category['parentId'] != null) { $temp = CharacteristicModel::find()->where(['idCategory' => $category['parentId']])->all(); foreach ($temp as $temp_char) { $characteristics[] = $temp_char; } $category = CategoryModel::find()->where(['id' => $category['parentId']])->one(); } } $counter = 0; foreach ($characteristics as $characteristic) { $char_model = CharacteristicValueModel::find()->where(['idProduct' => $model['id'], 'idCharacteristic' => $characteristic->id])->one(); if ($char_model == null) { $char_model = new CharacteristicValueModel(); $char_model->idProduct = $model['id']; $char_model->idCharacteristic = $characteristic->id; } echo Html::activeHiddenInput($char_model, "[{$counter}]idProduct"); echo Html::activeHiddenInput($char_model, "[{$counter}]idCharacteristic"); echo $form->field($char_model, "[{$counter}]value", ['labelOptions' => ['label' => $characteristic->name . ' (' . $characteristic->value . ')']])->textInput(); $counter++; } } ?> <div class="form-group"> <?php