1

Im attempting to add a file upload field to a form in YII, while its succesfully submitting and uploading the file to the correct folder, its not adding anything to the database.

Im only learning this platform so any guidance would be great.

Here is my view...

<div class="row">
  <div class="span4"><?php echo $form->labelEx($model,'slider_image'); ?></div>
  <div class="span5"><?php echo $form->fileField($model,'slider_image'); ?></div>
  <div class="span3"><?php echo $form->error($model,'slider_image'); ?></div>
</div>  

Here is my controller...

public function actionEdit() {
    $id = Yii::app()->getRequest()->getQuery('id');
    $model = CustomPage::model()->findByPk($id);
    if (!($model instanceof CustomPage)) {
        Yii::app()->user->setFlash('error',"Invalid Custom Page");
        $this->redirect($this->createUrl("custompage/index"));

    }
    if(isset($_POST['CustomPage'])) {
        $model->attributes = $_POST['CustomPage'];
        $model->image=CUploadedFile::getInstance($model,'slider_image');

        if ($model->validate())    {
            if ($model->deleteMe) {
                $model->delete();
                Yii::app()->user->setFlash('info',"Custom page has been deleted");
                $this->redirect($this->createUrl("custompage/index"));
            } else {
                $model->request_url =  _xls_seo_url($model->title);
                if (!$model->save()) {
                    Yii::app()->user->setFlash('error',print_r($model->getErrors(),true));
                } else {
                    $model->image->saveAs(Yii::app()->baseUrl.'images/'.$model->image);
                    Yii::app()->user->setFlash('success',
                        Yii::t('admin','Custom page updated on {time}.',array('{time}'=>date("d F, Y  h:i:sa"))));
                    $this->beforeAction('edit'); //In case we renamed one and we want to update menu
                }
            }
        }
    }
}

and my model

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
                    // other rules
        array('slider_image', 'file', 'types'=>'jpg, gif, png'),
    );
}

The form itself overall is working fine, unfortunately I dont understand how YII adds to the database

Thanks

Adrian

EDIT: Ive also obviously got a slider_image field in that table

2
  • 1
    what your Controller code would do is save the file name of the upoloaded file in your database table. Another thing is: your code $model->image=CUploadedFile::getInstance($model,'slider_image'); is referring to the wrong attribute. I think it should be $model->slider_image=CUploadedFile::getInstance($model,'slider_image'); Finally, you need to call $model->slider_image->save('path.to.file'); in order to save the file to disk. Commented Sep 30, 2013 at 11:44
  • Glad I could help. Just posted the solution as an answer so others can find it easily. Commented Sep 30, 2013 at 13:37

2 Answers 2

2

What your Controller code would do is save the file name of the upoloaded file in your database table. Another thing is: your code:

$model->image=CUploadedFile::getInstance($model,'slider_image');

is referring to the wrong attribute. I think it should be:

$model->slider_image=CUploadedFile::getInstance($model,'slider_image');

Finally, you need to call $model->slider_image->save('path.to.file'); in order to save the file to disk

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you get stock at $model->validate(). Because you do copy image above this line copying is fine but you do not go through validation so it never saves to DB.

var_dump($model->validate()); to see what is going on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.