2014-04-15 41 views
3

這裏地處myyiiapp \後端代碼\意見\ product_form.php如何實現卡爾蒂克yii2的FileInput的形式,使用不同的模型

<?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列。

如何設置屬性的地方說了圖像someAttributeName這是ProductImage模式,這種形式是使用產品模型。總之,我們怎麼能在這裏使用多個模型,我需要先創建產品,然後是圖像路徑,因爲爲了保存圖像路徑,我需要由mysql自動生成的產品ID。

我已經生成了從Yii2 crud的一切。

回答

2

最後,經過太多的試驗和錯誤,我找到了我自己的問題的解決方案。

查看:_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($文件)這將顯示上傳的圖像數據的輸出。

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 
相關問題