Jump to content
Larry Ullman's Book Forums
lrzins

Renaming An Uploaded File Using $Model->Id

Recommended Posts

Hi Larry, on page 216 of your Yii book, you describe how to rename an uploaded file using the $model->id of the User model. I think this is a good idea, but I don't see how your code can work without an afterSave or something.

 

Here is your code:

$model->avatar->saveAs($dest . '/' . $model->id . '.' .
    $model->avatar->extensionName);

The line above would have to come after the code to save the model:

if ($model->save())

because you don't obtain the $model->id until after you save it.

 

But since you've already saved the file's name to the database, the filename in the database and the name of the uploaded file don't match.

 

And you should'nt save the uploaded file in the file system until you know that the save to the database was successful.

 

Do you see what I mean? Am I missing something?

 

Larry Z.

 

 

Share this post


Link to post
Share on other sites

Yes, you would have to do that after saving the model. That's shown on the previous pages. Sorry it's not more clear. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...