How to change file name before saveing it to DB

Hallo!!

I’m looking for some way to change file name before saving it to database. I know how to change file name using saveAs(), but I’d like to change file name in DB as well. Here is my code for create action:


/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Company;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

			$folder=Yii::getPathOfAlias('webroot').'/images/company/';// folder for uploaded files

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

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

			

			if($model->save())

				{

					if(isset($model->logo))

						{

							if(!is_dir($folder.$model->id)){

								mkdir($folder.$model->id);

							}

							$model->logo->saveAs($folder . $model->id . '/'.$model->id .'_'.$model->update_time.'_'.$model->logo);

						}

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

				}

		}


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

			'model'=>$model,

		));

	}

in this case my file name is: $model->id .’’.$model->update_time.’’.$model->logo, so lets say 1_123456789_file.jpg, but to database only file.jpg is written. Any hints how to change my controller to change file name before save. I’ve been looking around for solution, but couldn’t find.

Regards lukBB

Use a temporary variable for CUploadedFile::getInstance($model,‘logo’);… and assign the file name you need to the $model->logo…

something like




$file=CUploadedFile::getInstance($model,'logo');

$model->logo=$model->id.'_'.$model->update_time.'_'.$file->name;

...



To save with the same name use:


$file->saveAs($folder.$model->id.'/'.$model->logo);

Thank you mdomba, all work great!!

;)