Massive assignment & CUploadedFile

Hello,

I understand that in a model we can assign a CUploadedFile instance to an attribute like that :




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



But is there any way, we can use the massive assignment to load the CUploadedFile instance in the $model->file attribute ?

It sounds to me logic since the model should handle the data. Something like :




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



And the file attribute is populate with a CUploadedFile instance.

Maybe is there a ‘onMassiveAssignment’ method to achieve that ?

Thanks for help !

Maxime.

may be you should write a validate method in your model to handle the uploading。

or extends the CFileValidator to perform the uploading in that class :lol:

but i think just keep it is . the solution yii give us is sophistication

Hello,

Thanks for the reply :)

I’m not sur I can write a validate method since I need the attribute to be populated before the validation.

Maybe I can override the setAttributes method.

and so something like :




public function setAttributes($values,$safeOnly=true)

	{

		if(!is_array($values))

			return;

		$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());

		foreach($values as $name=>$value)

		{

			if(isset($attributes[$name]))

                        {

                                if(isset($name['file']))

                                        $this->$name=CUploadedFile::getInstance($model,'file');

                                else

         				$this->$name=$value;

                        }

			else if($safeOnly)

				$this->onUnsafeAttribute($name,$value);

		}

	}



What do you think ?

Maxime.

There’s no need to extend CFileValidator. As documentation says:

Hello,

Thanks for the reply.

I tried to create a form field with the name "file" then to create a rule in my model that check for a JPG, PNG or GIF in the file attribute.

But no success :(

But, I realized that assigning something to an attribute is not data manipulation and belongs to the controller. I guess I will only move the file operations (save) to the model (which is data manipulation) :)

Maxime.