Problem With Beforesave

Hello again.

I’m having a problem with a beforeSave function that does not seem to even execute. I want to save three PDF files into the database and here is the code:

AVAIABLE COLUMNS:


 * @property string $NamePDF1

 * @property string $PDF1

 * @property string $NamePDF2

 * @property string $PDF2

 * @property string $NamePDF3

 * @property string $PDF3

VARIABLES FOR UPLOAD:


public $uploadFile1;

public $uploadFile2;

public $uploadFile3;

RULES:


			array('uploadFile1', 'file', 'types'=>'pdf', 'allowEmpty' => true),

			array('uploadFile2', 'file', 'types'=>'pdf', 'allowEmpty' => true),

			array('uploadFile3', 'file', 'types'=>'pdf', 'allowEmpty' => true),

BEFORESAVE FUNCTION:


	public function beforeSave()

	{

		if($file=CUploadedFile::getInstance($this,'uploadFile1'))

		{

			$this->NamePDF1=$file->name;

			$this->PDF1=file_get_contents($file->tempName);

		}

		if($file=CUploadedFile::getInstance($this,'uploadFile2'))

		{

			$this->NamePDF2=$file->name;

			$this->PDF2=file_get_contents($file->tempName);

		}

		if($file=CUploadedFile::getInstance($this,'uploadFile3'))

		{

			$this->NamePDF3=$file->name;

			$this->PDF3=file_get_contents($file->tempName);

		}

		return parent::beforeSave();

	}

FORM VIEW:


<div class="row">

		<?php echo $form->labelEx($model,'uploadFile1'); ?>

    	<?php echo $form->fileField($model,'uploadFile1'); ?>

		<?php echo $form->error($model,'uploadFile1'); ?>

	</div>

	<div class="row">

		<?php echo $form->labelEx($model,'uploadFile2'); ?>

    	<?php echo $form->fileField($model,'uploadFile2'); ?>

		<?php echo $form->error($model,'uploadFile2'); ?>

	</div>

	<div class="row">

		<?php echo $form->labelEx($model,'uploadFile3'); ?>

    	<?php echo $form->fileField($model,'uploadFile3'); ?>

		<?php echo $form->error($model,'uploadFile3'); ?>

	</div>

I do not know what I am missing here. The only thing I did is that I write the avaiable columns by hand. Anywhere else that I should add code? This should work…

You should probably post your action code too.

No problem. Action is in no way altered as it was gii generated:


public function actionCreate()

	{

		$model=new OsnovnoSredstvo;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}

If you’re sure that beforeSave() isn’t being called, that would suggest that validation is failing. I think you need to use CUploadedFile::getInstance() before you call save, or it won’t be validated as a file.

Have a look at the controller part of the guide here.

Found out the solution:

The problem was not in the beforeSave as I incorrectly supposed. The problem was in the _form view where htmlOptions were missing:


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'osnovno-sredstvo-form',

	'enableAjaxValidation'=>false,

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

)); ?>

Sorry for missguide you. It is hard to find out the solution if no errors are written anywhere.