0

I'm implementing some file uploading fields and i'm having some difficulties with it. I was following this article and everything was perfect until I tried to update my model. If I didn't fill the file field, it was cleared after save. I was googling and find this topic. I've modified my code, but now, when I try to modify another field, it isn't save, unless I modify the image field too. Where's the problem? My model code:

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Company']))
        {
            $model->attributes=$_POST['Company'];
            $the_image = CUploadedFile::getInstance($model,'image');
            if (is_object($the_image) && get_class($the_image)==='CUploadedFile')
              $model->image = $the_image;
            if($model->save())
                if (is_object($the_image))
                $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/upload/' . $model->image);
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
    }

And these are the rules in my model:

            array('name', 'required'),
            array('type, number, rating, user_id', 'numerical', 'integerOnly'=>true),
            array('name, image, address, site, mail', 'length', 'max'=>1000),
            array('text', 'safe'),
            array('image', 'file', 'types'=>'jpg, gif, png'),
            array('image', 'unsafe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, type, name, text, address, site, mail, number, rating, user_id', 'safe', 'on'=>'search'),

Please, help me. I have no clue why it isn't working.

2 Answers 2

3

Try modifying your image attribute rule on the Update scenario like this:

array('image', 'file', 'types'=>'jpg, gif, png','allowEmpty' => true, 'on'=>'update'),

I found this tip here, hope it works for you.

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

Comments

0
public function uploadMultifile ($model,$attr,$path)
        {
            if($sfile=CUploadedFile::getInstances($model, $attr)){
              foreach ($sfile as $i=>$file){  
                 $formatName=time().$i.'.'.$file->getExtensionName();
                 $file->saveAs(Yii::app()->basePath  .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
                     //  $model->image->saveAs(Yii::app()->basePath . '/../studentimage/' . $date . $model->image);
                 $ffile[$i]=$formatName;
                 }
                return ($ffile);
             }
         }

public function actionCreate()
{
    $model=new Subject;
    // $model->date_erected = date('Y-m-d');
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Subject']))
    {
        $model->attributes=$_POST['Subject'];
                    if($filez=$this->uploadMultifile($model,'imagea','/../images/views/'))
                {
                    $model->documents=implode(",", $filez);
                }
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

1 Comment

When answering questions, it would be nice if you called out what was changed.

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.