How to insert data into multiple tables in Yii

advertisements

I am new in Yii. I need to create product module to save product data. In that i need to create two table products and product_image to save product data and multiple product images.

Product table

id, category_id, , title ,price, description

Product image table

id, product_id, image

I have created table as above and generated model and CRUD for product table. BUT when I goto add product page I am not getting image upload button. I am getting just product table field in add product page.

Should I created model for two table to get image upload button ?

How to insert data in multiple table in yii ?

thanks in advance.

UPDATES

ProductController :

public function actionCreate()
    {
        $model = new Products();
        $productsImage = new ProductsImage();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'productsImage'=> $productsImage,
            ]);
        }
    }

My Add product form.php

<div class="products-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'category_id')->dropDownList(['0'=>'Select Parents Category']+
        ArrayHelper::map(ProductCategory::find()->all(),'id','category_name')
    ) ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'price')->textInput() ?>

    <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

    <?= $form->field($productsImage,'image')->fileInput() ?> //here i am getting error of undefined variable $productsImage

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


In Your products table there is no field for image so you are not getting upload image button. Here is many way you can do this. 1 ) You can create public variable for image button in you products model. suppose you have create public variable in products model like public $productImage and in view file in you can create upload button for image using this code.

`echo $form->fileField($model,'productImage',array('class' => 'btn'));`

2) Second option if you want create productImage model then you need to pass that object from create action of product. suppose in your products controller in create action you can create object of productImages model like below.

$productImages = new productImages;

and you need to pass this model variable in create view like this way

 $this->render('create',array(
                'model'=>$model,
                'productImages'=> $productImages,

                ));

Here suppose $model is your products model object. and in view file you can create image button like below code.

echo $form->fileField($productImages,'product_image',array('class' => 'btn'));

here 'product_image' is your column name of your product_image table. in controller create action you will get in post object using product_images post after that save data how you want i hope you will get idea what you need to do now. hope it helps you