Yii2:图像存储在不同的表中

Yii2: Images stored in different tables

这是我的控制器:

    $model = new VehicleType();
    if ($model->load(Yii::$app->request->post())) {
        if($model->validate()){

            $model->save();
            $id = $model->id;
            $model->file = UploadedFile::getInstance($model, 'file');
            if($model->file){
                $id = $model->id;
                $imageName = "vehicletype_".$id.'_'.getdate()[0];
                $model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);

                $station = VehicleType::findOne($id);
                $station->image = '@web/uploads/'.$imageName.'.'.$model->file->extension;
                $station->save();
            }
            return $this->redirect(['vehicletype/index']);
        }
    } else {
        return $this->renderAjax('create', [
            'model' => $model,

        ]);
    }
}

我的看法:

 <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'station-form', 'options' => ['enctype' => 'multipart/form-data']]); ?>
                <?= $form->field($model, 'name') ?>              

                <?= $form->field($model, 'description')->textarea() ?>
                <?= $form->field($model, 'file')->fileInput() ?>
                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>
            <?php ActiveForm::end(); ?>

        </div>

我的模特:

public function rules()
{
    return [
        [['description'], 'string'],
        [['record_status'], 'integer'],
        [['name', 'image'], 'string', 'max' => 255]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'name' => 'Name',
        'description' => 'Description',
        'image' => 'Image',
        'record_status' => 'Record Status',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getVehicles()
{
    return $this->hasMany(Vehicles::className(), ['vehicle_type_id' => 'id']);
}

}

有了这个我只能上传一张 picture/post,我想要一张 post 有多张图片,所以我创建了一个新的 table 调用 'Image' 来存储了我的照片并且有一对多的关系。

但是我 运行 遇到了一个问题,我怎样才能将数据从 1 个表单 table 添加到 2 tables

我正在使用 Yii2 基本模板

谢谢

步骤 1

首先在您的 vehicletype 模型中创建两个变量。

        public $uploadedImages;
        public $imageforNewtable = array();

步骤 2

确保在您的模型规则中提及此 imageforNewtable 变量。

        [['imageforNewtable'], 'image', 'extensions' => 'png, jpg, JPEG'],

步骤 3

在您的表单中:

        <?php $form = ActiveForm::begin(['id' => 'station-form', 'options' => ['enctype' => 'multipart/form-data']]); ?>


        <?= $form->field($model, 'imageforNewtable[]')->fileInput(['accept' => 'image/*']); ?>

步骤 4

在您的控制器中:

$model->uploadedImages = UploadedFile::getInstances($model,'imageforNewtable');

        // Make sure to put "Instances" (plural of Instance) for uploading multiple images

$model->imageforNewtable = array(); // To avoid blank entries as we have entries in $_FILES not in $_POST.

            foreach ($model->uploadedImages as $singleImage) 
            {
            $model->imageforNewtable[] = time() . '_' . $singleImage->name;
            }

现在批量插入数据到 Images table :

    $bulkInsertforimages = array(); //defined benchInsert array for images
    $columnsArray = ['blog_id', 'image']; //Column names in which bulk insert will take place.                
    if ($model->imageforNewtable != '') {
        foreach ($model->imageforNewtable as $singleData) {
            $bulkInsertforimages[] = [
                'blog_id' => $model->id,
                'image' => $singleData,
            ];
        }
    }


    if (count($bulkInsertforimages) > 0) {
        $command = \Yii::$app->db->createCommand();
        $command->batchInsert('YOUR_IMAGE_TABLE', $columnsArray, $bulkInsertforimages)->execute();
    }