I am having trouble uploading an image to the db. The db table always shows [BLOB - 0 B] when a record is saved. I have tried manually uploading an image to the table, and that works. The validation seems to be working. After save() I link between profile table and image table, and that works just fine. Any insights would be very helpful.
Image Model:
['org_picture', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'maxFiles' => 1, 'maxSize' => 1024 * 100, 'skipOnEmpty' => true, 'on' => 'name_desc'],
// ...
public function handleForm2()
{
if ($this->validate() && $this->save()) {
return $this;
}
return false;
}
Profile Model:
public function handleForm2($image)
{
if ($this->validate() && $this->save()) {
$this->link('image', $image);
return $this;
}
return False;
}
Controller:
$profile = profile::findOne($profileId);
if (!isset($profile)) {
throw new NotFoundHttpException("The requested profile was not found.");
}
$image = new image();
if ($image->load(Yii::$app->request->Post()) &&
$image->handleForm2() &&
$profile->load(Yii::$app->request->Post()) &&
$profile->handleForm2($image)) {
return isset($_POST['continue']) ? $this->redirect(['form3', 'profileId' => $profile->id]) : $this->goHome();
} else {
return $this->render('form2_name_desc', ['profile' => $profile, 'image' => $image]);
}
View:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($profile, 'org_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($profile, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($image, 'org_picture')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Save & Continue', [
'method' => 'POST',
'class' => 'btn btn-primary',
'name' => 'continue',
]) ?>
</div>
<div class="form-group">
<?= HTML::submitbutton('Save & Exit', [
'method' => 'POST',
'class' => 'btn btn-primary',
'name' => 'exit',
]) ?>
</div>
<?php ActiveForm::end(); ?>