如何以使用不同模型的形式实现kartik yii2 FileInput [英] How to implement kartik yii2 FileInput in form which is using different model

查看:70
本文介绍了如何以使用不同模型的形式实现kartik yii2 FileInput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是位于myyiiapp \ backend \ views \ product_form.php中的代码

Here is the code located in myyiiapp\backend\views\product_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\widgets\FileInput;

/**
 * @var yii\web\View $this
 * @var backend\models\Product $model
 * @var yii\widgets\ActiveForm $form
 */
?>

<div class="product-form">

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

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

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

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

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

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

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

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

    <?=
    // Usage with ActiveForm and model
     $form->field($model, 'someAttributeName')->widget(FileInput::classname(), [
        'options' => ['accept' => 'image/*'],
    ]);

    ?>

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

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

</div>

我有一个表名"产品",还有另一个表名" product_images ",其中包含产品图像路径和产品ID列.

I have a table name "product" and I have another table name "product_images" which contain product image path and product id column.

如何为ProductImage模型中的图像设置属性,并在其上显示 someAttributeName ,该表单使用的是 Product 模型.简而言之,我们如何在这里使用多个模型,我需要先创建产品,然后创建图像路径,因为要保存图像路径,我需要将由mysql自动生成的产品ID.

How can I set attribute where it says someAttributeName for image which is in ProductImage model and this form is using Product model. In short how can we use multiple model in here and I need to create product first then image path because to save image path I need product id which is going to be auto generated by mysql.

我已经从Yii2 crud生成了一切.

I have generated everything from Yii2 crud.

推荐答案

最后,经过反复的试验,我找到了自己问题的解决方法.

Finally after too many trial and error I've found the solution of my own question.

查看:_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\widgets\FileInput;

/**
 * @var yii\web\View $this
 * @var backend\models\Product $model
 * @var yii\widgets\ActiveForm $form
 */
?>

<div class="product-form">

    <!--change here: This form option need to be added in order to work with multi file upload ['options' => ['enctype'=>'multipart/form-data']]-->
    <?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>

    <?= $form->field($model, 'category_id')->dropDownList($model->getCategoryList()) ?>

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

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

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

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

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

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

    <?php

    // Usage with ActiveForm and model
    //change here: need to add image_path attribute from another table and add square bracket after image_path[] for multiple file upload.
     echo $form->field($productImages, 'image_path[]')->widget(FileInput::classname(), [
        'options' => ['multiple' => true, 'accept' => 'image/*'],
        'pluginOptions' => [
            'previewFileType' => 'image',
            //change here: below line is added just to hide upload button. Its up to you to add this code or not.
            'showUpload' => false
        ],
    ]);
    ?>

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

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

</div>

控制器

public function actionCreate()
    {
        $model = new Product;
        $productImages = new ProductImages;

        if($_POST){
            //below line will fetch all image related data and put it into $file as an object. Refer output of var_dump($file) after controller code.
            $file = UploadedFile::getInstances($productImages, 'image_path');
            var_dump($file);
        }

        //below code is where you will do your own stuff. This is just a sample code need to do work on saving files
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'productImages' => $productImages,
            ]);
        }
    }

var_dump($ file).这将显示上传的图像数据的输出.

var_dump($file) This will show output of uploaded image data.

array (size=3)
  0 => 
    object(yii\web\UploadedFile)[45]
      public 'name' => string '1.jpg' (length=5)
      public 'tempName' => string 'D:\wamp\tmp\php8E46.tmp' (length=23)
      public 'type' => string 'image/jpeg' (length=10)
      public 'size' => int 77593
      public 'error' => int 0
  1 => 
    object(yii\web\UploadedFile)[46]
      public 'name' => string '2.jpg' (length=5)
      public 'tempName' => string 'D:\wamp\tmp\php8E56.tmp' (length=23)
      public 'type' => string 'image/jpeg' (length=10)
      public 'size' => int 74896
      public 'error' => int 0
  2 => 
    object(yii\web\UploadedFile)[47]
      public 'name' => string '3.jpg' (length=5)
      public 'tempName' => string 'D:\wamp\tmp\php8E57.tmp' (length=23)
      public 'type' => string 'image/jpeg' (length=10)
      public 'size' => int 72436
      public 'error' => int 0

这篇关于如何以使用不同模型的形式实现kartik yii2 FileInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆