doushou3814 2016-05-05 06:17 采纳率: 100%
浏览 211
已采纳

如何在Yii中的多个表中插入数据

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>
  • 写回答

2条回答 默认 最新

  • dongyi1777 2016-05-05 06:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果