I have Groupdetails model, view and controller. Similarly I have memberdetails model, view and controller where one groupdetails can have many memberdetails (One to many relation).
In my Groupdetails table, I have a field, Photo which stores image name. Now In Groupdetails Model, I have declared a variable $filepath and rules as below:
Model:
class Groupdetails extends \yii\db\ActiveRecord
{
public $filepath;
public function rules()
{
return [
[['filepath'], 'file', 'extensions' => 'jpg, png'],
];
}
}
In Controller actionCreate is working fine. But in actionUpdate(), the image name saved in table is not displayed. Every time the user has to select the photo during updation.
Here is actionCreate()
public function actionCreate()
{
$model->filepath = UploadedFile::getInstance($model, 'filepath');
$model->Photo = $model->filepath->basename.'.'.$model->filepath->extension;
$model->save(false);
$model->filepath->saveAs('uploadedgroupimages/'.$model->filepath->basename.'.'.$model->filepath->extension);
}
Here is actionUpdate()
public function actionUpdate()
{
$model->filepath = UploadedFile::getInstance($model, 'filepath');
$model->Photo = $model->filepath->basename.'.'.$model->filepath->extension;
$model->save(false);
$model->filepath->saveAs('uploadedgroupimages/'.$model->filepath->basename.'.'.$model->filepath->extension);
}
On Update page, we again find the Choose file button which asks for image to be selected, because of which user has to select the photo every time during update case. The image name should also be displayed. In case if user does not want to update the photo then it should take the previous saved image.