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 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下: