public function actionCreate() { $model = new Items(); $upload = new UploadForm(); $category = new Categories(); $subCat = new SubCatgories(); $category->find()->all(); $subCat->find()->all(); $model->is_aviable = 1; $description = new Description(); $description->item_id = 1; $description->sub_id = 1; $description->text = "s"; $description->save(); if ($model->load(Yii::$app->request->post()) && $model->save()) { if (isset($_POST['Description'])) { // print_r($_POST['Description']['sub_name']); $subCat = new SubCatgories(); $subCat->find()->where(['name' => $_POST['Description']['sub_name']])->all(); $s = $subCat->find()->where(['name' => $_POST['Description']['sub_name']])->all(); $description = new Description(); $description->item_id = $model->id; $description->sub_id = $s[0]['id']; $description->text = $_POST['Description']['text']; $description->category_name = $_POST['Description']['category_name']; $description->sub_name = $_POST['Description']['sub_name']; $description->save(); // echo "sssss"; // echo $description->sub_id; // echo $description->item_id; } if (isset($_POST['Description'])) { $upload = new UploadForm(); $upload->imageFiles = UploadedFile::getInstances($upload, 'imageFiles'); if ($upload->upload()) { echo "upload"; // file is uploaded successfully // return; } } // else // return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'category' => $category, 'subCat' => $subCat, 'description' => $description, 'upload' => $upload]); } }
/** * @return \yii\db\ActiveQuery */ public function getSub() { return $this->hasOne(SubCatgories::className(), ['id' => 'sub_id']); }
<?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'price')->textInput(); ?> <?php echo $form->field($model, 'count')->textInput(); ?> <?php echo Html::activeDropDownList($description, 'category_name', ArrayHelper::map(\app\models\Categories::find()->all(), 'name', 'name')); ?> <?php echo Html::activeDropDownList($description, 'sub_name', ArrayHelper::map(\app\models\SubCatgories::find()->all(), 'name', 'name')); ?> <?php echo $form->field($description, 'text')->textarea(['rows' => 3])->label('Description'); ?> <?php echo $form->field($model, 'is_aviable')->textInput(); ?> <?php echo $form->field($upload, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getSubCatgories() { return $this->hasMany(SubCatgories::className(), ['category_id' => 'id']); }