Actionupdate With Uploadedfile

I’ve got a model that has to fields relating to files through UploadedFile. I have no problem populating these fields with the filename on actionCreate. However, actionUpdate invariably causes the fields to be nulled on update if no change is made. I’ve hunted all over to find how to handle an update action on such fields and could find precious little. Further, what I found doesn’t seem to work, at least in Yii2. Personally, I can’t imagine having a Web site that didn’t have a number of such fields, and I would never include a field in a database without a mechanism for easily updating the field.

I’m using the Kartik-V FileInput Widget, but the mechanism should be similar no matter how it’s done, and like virtually everyone else, he provides useful information on actionCreate but essentially nothing on actionUpdate.

For what it’s worth, here’s one of the multitudinous variations on attempts to resolve this problem, none of which have worked:


public function actionUpdate($id)

        {

            $model = $this->findModel($id);


            $originalImage = $model->primary_image;

            $originalFloorplan = $model->floorplan;


            if ($model->load(Yii::$app->request->post())) {


                $primaryImage = UploadedFile::getInstance($model, 'primary_image');

                $floorPlan    = UploadedFile::getInstance($model, 'floorplan');


                if (!empty($primaryImage)) {

                    $model->primary_image = $primaryImage->name;

                } else {

                    $model->primary_image = $originalImage;

                }


                if (!empty($floorPlan)) {

                    $model->floorplan = $floorPlan->name;

                } else {

                    $model->floorplan = $originalFloorplan;

                }


                $primaryImagePath = 'files/home_address_property/primary_image/' . $model->primary_image;

                $floorPlanPath    = 'files/home_address_property/floorplan/' . $model->floorplan;


                if ($model->save()) {


                    if (!empty($primaryImage)) {

                        $primaryImage->saveAs($primaryImagePath);

                    }


                    if (!empty($floorPlan)) {

                        $floorPlan->saveAs($floorPlanPath);

                    }


                    return $this->redirect(['view', 'id' => $model->id]);

                } else {

                    // Error handling

                }

            }


            return $this->render('update', ['model' => $model,]);


        }

As I said, that one doesn’t work. If I attempt an update to the model without entering something in the related file fields, I end up with null file fields in the database. Obviously, I don’t have to re-enter every single field when I update a record; why should I have to do that for a file field?

I really had hoped that, in the rewriting from Yii 1.1 to 2.x, Yii would have become a framework that actually simplified such common, everyday tasks that are necessary to virtually every Web site. I guess not.

Anyway, can anyone help me with this one? It just shouldn’t be so hard!