Yii Framework Forum: optional upload file when update a record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

optional upload file when update a record Rate Topic: ***** 2 Votes

#1 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 03 November 2009 - 08:52 PM

Hi guys,
I had some problems when I tried update a record on my application without upload again a file.
I have a form and on this form there are two fields for upload files, when I create a new record the valdation requires the files, but on update no.
When I try to update a record I receive the error:

Fatal error: Call to a member function saveAs() on a non-object in /Users/san/src/nagloria/trunk/web/public/protected/controllers/admin/RepertoryController.php on line 74


here are my actions:

  public function actionCreate()
  {
    $piece = new Repertory;
    $piece->scenario = 'create';
    $this->registration($piece, $_POST['Repertory']);
    $this->render('create', array('repertory' => $piece));
  }
  
  public function actionUpdate()
  {
    $piece = $this->loadpiece();
    $this->registration($piece, $_POST['Repertory']);    
    $this->render('update', array('repertory' => $piece));
  }

  private function registration($model, $form)
  {
    if(isset($form))
    {
      $model->attributes = $form;
      $model->media_file = CUploadedFile::getInstance($model, 'media_file');
      $model->thumb      = CUploadedFile::getInstance($model, 'thumb');
      if($model->save())
      {
        exec("mkdir -p " . Yii::app()->basePath . "/../files/repertory/media_file/{$model->id}/");
        exec("mkdir -p " . Yii::app()->basePath . "/../files/repertory/thumbs/{$model->id}/");
        $model->media_file->saveAs(Yii::app()->basePath . "/../files/repertory/media_file/{$model->id}/{$model->media_file->name}");
        $model->thumb->saveAs(Yii::app()->basePath . "/../files/repertory/thumbs/{$model->id}/{$model->thumb->name}");
        $this->redirect('/admin/repertory/list');
      }
    } 
  }



How can I fix up this?

Thanks?
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 03 November 2009 - 10:27 PM

I would try with
if (isset($model->media_file))
  $model->media_file->saveAs(Yii::app()->basePath . "/../files/repertory/media_file/{$model->id}/{$model->media_file->name}");
if (isset($model->thumb))
  $model->thumb->saveAs(Yii::app()->basePath . "/../files/repertory/thumbs/{$model->id}/{$model->thumb->name}");


(CUploadedFile::getInstance() returns null if no file was uploaded)

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 04 November 2009 - 07:16 AM

Hi tri,

I did your sugestion, the application not showed the error again but happened other error, when I updated a record without upload a file, the aplication stored a NULL value on database field rewriting the file name that was stored on this field first.

I need to persist the file name on database if files aren't uploaded.

Any sugestion?

Thanks.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 04 November 2009 - 08:05 AM

Hi GodFather, I think you can remove 'media_file' and 'thumb' from safeAttributes() in your 'update' scenario. Assign one by one if set.

(Not tested)

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 04 November 2009 - 11:47 AM

Hi tri,
I not understood you, I'm not using this attributes how safeattributes, actually I just Assing the values on they.

Do you think that I'm doing something wrong?
Could you show me a exemple?

Thanks.
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 04 November 2009 - 12:46 PM

View PostGodFather, on 04 November 2009 - 11:47 AM, said:

Hi tri,
I not understood you, I'm not using this attributes how safeattributes, actually I just Assing the values on they.

Do you think that I'm doing something wrong?
Could you show me a exemple?

Thanks.


I haven't tried this myself, but I think manual assignment of the two uploaded fields, if not empty, would be a better solution than to explicitly save the old values.
To me it seems like you massively assign from a form in this statement.
$model->attributes = $form;

Do you use 1.0 or 1.1? If the latter, you have to specify an 'update' scenario rule instead.

On second thought, why are the upload fields empty in your 'update' scenario? I think I'm going to try this out now. I'm sure somebody else can explain this better.

Edit:
Sorry, in my first answer I erroneously got the impression you saved to local vars. Now I see that you save to the model and I don't know the semantics of CUploadedFile. Please disregard my answer.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 04 November 2009 - 08:09 PM

Ok, here is how it works:

(myfile to be replaced by media_file, thumb)

$model->myfile is a string when read back from the db. It is massively assigned as I suspected (default is all attributes). If the getInstance call succeeds, $model->myfile becomes a CUploadedFile object (else it seems to becomes an instance of the current controller !?).

1) safeAttributes
public function safeAttributes()
{
return array(
  'create'=>'media_file, thumb, attr_1, attr_2, attr_n',
  'attr_1, attr_2, attr_n',
);
}


2) don't overwrite $model->file if invalid object
$myfile = CUploadedFile::getInstance($model,'myfile');
if (is_object($myfile) && get_class($myfile)==='CUploadedFile')
  $model->myfile = $myfile;


3) test before save
if (is_object($model->myfile))
  $model->Picture->saveAs($mypath.'/'.$model->myfile->name);


The logic can be improved (don't create dir when not neccessary, clear $model->myfile if file cannot be saved, filesize rules)

Hope I got it right this time.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
3

#8 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 05 November 2009 - 10:42 AM

Man, all is running right now.
Thanks for your help, and sorry by the time that you spent with this post.

Thanks alot.
0

#9 User is offline   Denzil Sequeira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 09-February 12
  • Location:Mumbai,India

Posted 12 February 2012 - 01:16 AM

Thanks alot tri really helped me ............ :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users