Update Without Upload

Hi there.

I have filefield(foto). When I don’t what to change uploadedfile on update and keep old one I get error. How should I change it ? When I upload new file it’s ok.

Controller:


public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Pupil']))

		{

                        $path =Yii::app()->baseUrl.'/../../uploads/';

			$model->attributes=$_POST['Pupil'];

                        $model->fullname = $model->firstname.' '.$model->lastname;

                        

                        $model->start_time = $model->start_hour.':'.$model->start_min;

                        $model->end_time = $model->end_hour.':'.$model->end_min;

                        

                        if(isset($_POST['Pupil']['foto']))

                        {

                        $rnd = rand(0,999999);

                        $length = 10;

                        $chars = array_merge(range(0,9), range('a','z'), range('A','Z'));

                        shuffle($chars);

                        $randomChar = implode(array_slice($chars, 0, $length));

                        $uploadedFile=CUploadedFile::getInstance($model,'foto');

                        $fileName = "{$randomChar}-{$rnd}-{$randomChar}-{$uploadedFile}";  // random number + file name

                        $model->foto = $fileName; 

                        

			if($model->save())

                                $uploadedFile->saveAs(Yii::app()->basePath.'/../../uploads/'.$fileName);

				$this->redirect(array('view','id'=>$model->pupilId));

                        }

                        else {

                            //What should I change ?

                            if($model->save())

                                $this->redirect(array('view','id'=>$model->pupilId));

                        }

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}

Model:


public function rules()

	{

		

		return array(

			array('contract_contractId', 'required'),

                        array('foto','file','types'=>'jpg,png,pdf,doc,dot,docx,docm,dotx,xls,xlsm,xltm,ppt,pptm,ppsx,xlsx','allowEmpty'=>true),

			array('start_hour, start_min, end_hour, end_min, birth_date, start_time, end_time, foto', 'safe'),

                        array('foto','file','allowEmpty'=>true,'on'=>'update'),

                       

			

		);

	}

This is untested, but I would probably make these changes to the actionUpdate controller


public function actionUpdate($id) {

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

	

	if(isset($_POST['Pupil'])) {

		$path =Yii::app()->baseUrl.'/../../uploads/';

		$model->attributes=$_POST['Pupil'];

		$model->fullname = $model->firstname.' '.$model->lastname;

		

		$model->start_time = $model->start_hour.':'.$model->start_min;

		$model->end_time = $model->end_hour.':'.$model->end_min;

		

		$uploadedFile=CUploadedFile::getInstance($model,'foto');

		

		if((is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile')){

			$rnd = rand(0,999999);

			$length = 10;

			$chars = array_merge(range(0,9), range('a','z'), range('A','Z'));

			shuffle($chars);

			$randomChar = implode(array_slice($chars, 0, $length));

			$fileName = "{$randomChar}-{$rnd}-{$randomChar}-{$uploadedFile}";  // random number + file name

			if($uploadedFile->saveAs(Yii::app()->basePath.'/../../uploads/'.$fileName)){

				$model->foto = $fileName;

			}

		}

		

		if($model->save()) {

			$this->redirect(array('view','id'=>$model->pupilId));

		}


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

    }

}

This would check to see if the user has uploaded a new file into the $uploadedFile variable and only if so will it save the new file and change the ‘foto’ data in the database.

hello mentorq…

which is the error displayed? (cual es el error mostrado ?)

simply does not load the image? ( simplemente no se carga la imagen)

The page is not loading when I want to save without upload a file. Error 500 ;/

Thx for reply but still not working. Page is simply not loading. Error 500 and this is all I get ;/

Hmm… I think that only thing I can do is to create separate table for files and then get it by relations. ;/

hello mentorq, your view code ? … .your form have the "MULTIPART FORM DATA" property ?

or the problem is ONLY in the UPDATE ?

Yes I have


'htmlOptions' => array('enctype' => 'multipart/form-data'),

Problem appears only when I want to update any data without image upload. In details it looks like I can only update when I will once again send image.

I implemented this exact feature in my last project.

Please see this wiki article and more importantly, this user comment within the wiki article.

Also, this forum post.

I think setting your image upload form field to "unsafe" in the model rules is important to making this work.

I don’t know how I could miss that … thx you it`s exactly what I need. Cheers ;)

try this after inset the record


$model= Pupil::model("foto='".$_FILES['Pupil']['foto']."' AND id='".$id."'");

if(count($model)>0){

	//code here

}