Massive assignment & CUploadedFile


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


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 :


And the file attribute is populate with a CUploadedFile instance.

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

Thanks for help !


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


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)




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

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









			else if($safeOnly)




What do you think ?


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


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) :)