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>